Dictionary using File Processing - C Program

Language

A dictionary written in C that retrieves words from a text file. This is a practical exercise in file processing using text files. If a word that has been searched is not found in the dictionary, it is added at the end. The words in the dictionary are not in alphabetical order. Rather, they are randomly arranged. This is version 2 of a previous Dictionary. Enjoy
  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];
  6. char word_to_be_searched[20], *pointer_dictionary_array, *pointer_word_to_be_searched, loop_program_character;
  7. int counter, input_counter;
  8. FILE *file_pointer;
  9.  
  10.  
  11. /* Prototyping of functions*/
  12. char* search_dictionary_for_word(char *pointer_dictionary_array, char *pointer_word_to_be_searched);
  13. char* add_word_in_dictionary (char *pointer_dictionary_array, char *pointer_word_to_be_searched);
  14.  
  15.  
  16. int main() /* The main method*/
  17. { /* Brief details on the screen about the program*/
  18. 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 * Version 2 (Using files) *\n *************************************************************************/\n");
  19.  
  20. /* Populate the dictionary from the file */
  21. if ( (file_pointer = fopen("dictionary.txt", "r") ) == NULL )
  22. { printf( "File could not be opened.\n" );}
  23. else
  24. {
  25. if(file_pointer !=EOF)
  26. { printf("The words available in the dictionary are :\n");
  27. for( input_counter=0; input_counter<1000; input_counter++)
  28. {
  29. while((fscanf(file_pointer, " %[^ ]s", &dictionary_array[input_counter])) > 0)
  30. {
  31. printf("%10s\t",dictionary_array[input_counter]);
  32. input_counter += 1;
  33. }
  34. }
  35. }
  36. }
  37.  
  38.  
  39.  
  40. do
  41. { counter=0;
  42. printf("\n\nPlease enter the word to be searched :\n"); /* Prompt to instruct use to enter the string to be searched*/
  43. scanf("%s",&word_to_be_searched); /* Capture of the search string by the system*/
  44. search_dictionary_for_word(dictionary_array, word_to_be_searched);
  45.  
  46. printf("\n------------------------------------------------------------------------------\nDo you want to search for another word? \nPress Y to continue or any other \nto exit : ");
  47. scanf(" %c", &loop_program_character);
  48. printf("\n------------------------------------------------------------------------------\n");
  49. }
  50. while (loop_program_character=='y'||loop_program_character=='Y');
  51.  
  52. return 0;/* An indication that the program runs successfully*/
  53. } /* The end of the main method*/
  54.  
  55. char* search_dictionary_for_word(char *pointer_dictionary_array, char *pointer_word_to_be_searched)
  56. { while (*pointer_dictionary_array!='\0') /* Until the end of array character is encountered*/
  57. {
  58. if ((strcmp(pointer_dictionary_array, pointer_word_to_be_searched))==0)
  59. { 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*/
  60. return 0;
  61. }
  62.  
  63. else {
  64. //printf("Searched for \"%3s\" but found \"%3s\" %-5s\t\t%5d\n", word_to_be_searched, pointer_dictionary_array, " at position", pointer_dictionary_array);
  65. counter = counter+1;
  66. pointer_dictionary_array= &dictionary_array[counter][0]; /* Pointer moves to the next index of the array of characters (string)*/
  67. }
  68. }
  69.  
  70. add_word_in_dictionary (dictionary_array, word_to_be_searched); /* Calling the function add_word_in_dictionary() */
  71. return pointer_dictionary_array;
  72. }
  73.  
  74. char* add_word_in_dictionary (char *pointer_dictionary_array, char *pointer_word_to_be_searched)
  75. {
  76. pointer_dictionary_array=&dictionary_array[counter][0];
  77. strcpy(pointer_dictionary_array, pointer_word_to_be_searched);
  78. 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);
  79. return pointer_dictionary_array;
  80. }

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

Submitted bybabai (not verified)on Mon, 09/29/2014 - 13:00

enti babai nuvvu kottina code inta chetta ga undi denemma jeevitam anduko asala aa linelu enti nuvvu horizontal ga ye yedava aina 434 lines pedtada asala nee tala lopala emina unda laakunte edina oka panikirani code kotukoni tirugutunnava eesari nunchi code kotte tapudu indentation nerchuko babai

Add new comment