Searching for a word in a Dictionary - C Program

Language

A program that searches for a word in a dictionary. - Create a small dictionary of strings - Not in order - Create an interface for user to search for a word in the dictionary If the word is not found, the word is added to the dictionary. The dictionary has 1000 words (English/Swahili)
  1. #include <stdio.h>/* C Standard Input and Output Library*/
  2. #define array_size 1000 /* Constant size of the array to be used in storage of strings that makes up the dictionary*/
  3.  
  4. /* Variable declarations */
  5. char dictionary_array[array_size][20]={"david", "enoch", "cheese", "donkey", "orange", "banana", "child", "globe", "family", "aeroplane", "vehicle", "shell", "plant", "treatment"};
  6. char word_to_be_searched[20], *pointer_dictionary_array, *pointer_word_to_be_searched, loop_program_character;
  7. int counter;
  8.  
  9. /* Prototyping of functions*/
  10. char* search_dictionary_for_word(char *pointer_dictionary_array, char *pointer_word_to_be_searched);
  11. char* add_word_in_dictionary (char *pointer_dictionary_array, char *pointer_word_to_be_searched);
  12.  
  13.  
  14. int main() /* The main method*/
  15. { /* Brief details on the screen about the program*/
  16. printf(" /*************************************************************************\n * Mureithi David Wachira *\n * P15/2204/2011 *\n * *\n * University of Nairobi *\n * School of Computing & Informatics *\n * *\n * Course: PROGRAMMING AND PROBLEM SOLVING (CSC 121) *\n * Date: Tuesday 02nd April 2013 *\n * *\n * A program that searches for a word in a *\n * dictionary. *\n * - Create a small dictionary of strings *\n * - Not in order *\n * - Create an interface for user to search *\n * for a word in the dictionary *\n * *\n * If the word is not found, the word is added to *\n * the dictionary. *\n * The dictionary has 1000 words (English/Swahili) *\n * *\n *************************************************************************/\n");
  17.  
  18. do
  19. { counter=0;
  20. printf("Please enter the word to be searched :\n"); /* Prompt to instruct use to enter the string to be searched*/
  21. scanf("%s",&word_to_be_searched); /* Capture of the search string by the system*/
  22. search_dictionary_for_word(dictionary_array, word_to_be_searched);
  23.  
  24. printf("\n------------------------------------------------------------------------------\nDo you want to search for another word? \nPress Y to continue or any other \nto exit : ");
  25. scanf(" %c", &loop_program_character);
  26. printf("\n------------------------------------------------------------------------------\n");
  27. }
  28. while (loop_program_character=='y'||loop_program_character=='Y');
  29.  
  30. return 0;/* An indication that the program runs successfully*/
  31. } /* The end of the main method*/
  32.  
  33. char* search_dictionary_for_word(char *pointer_dictionary_array, char *pointer_word_to_be_searched)
  34. { while (*pointer_dictionary_array!='\0') /* Until the end of array character is encountered*/
  35. {
  36. if ((strcmp(pointer_dictionary_array, pointer_word_to_be_searched))==0) //&&(strlen(pointer_dictionary_array)!=strlen(pointer_word_to_be_searched)) &&(*pointer_dictionary_array-*pointer_word_to_be_searched!=0))
  37. { printf("\nThe word \"%s\" is in the dictionary at position %d\n",word_to_be_searched, pointer_dictionary_array); /* Ouput to the screen to show that the two strigns are equal*/
  38. return 0;
  39. }
  40.  
  41. else {
  42. printf("Searched for \"%3s\" but found \"%3s\" %-5s\t\t%5d\n", word_to_be_searched, pointer_dictionary_array, " at position", pointer_dictionary_array);
  43. counter = counter+1;
  44. pointer_dictionary_array= &dictionary_array[counter][0]; /* Pointer moves to the next index of the array of characters (string)*/
  45. }
  46. }
  47.  
  48. add_word_in_dictionary (dictionary_array, word_to_be_searched); /* Calling the function add_word_in_dictionary() */
  49. return pointer_dictionary_array;
  50. }
  51.  
  52. char* add_word_in_dictionary (char *pointer_dictionary_array, char *pointer_word_to_be_searched)
  53. {
  54. pointer_dictionary_array=&dictionary_array[counter][0];
  55. strcpy(pointer_dictionary_array, pointer_word_to_be_searched);
  56. printf("\nThe word \"%s\" has not been found in the dictionary but has been added \nin the dictionary at position %d\n",pointer_word_to_be_searched, pointer_dictionary_array);
  57. return pointer_dictionary_array;
  58. }
  59.  
  60. <c>

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Add new comment