Dictionary using File Processing - C Program

Submitted by: 
Visitors have accessed this post 10340 times.

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*/
  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;
  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);
  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");
  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. }
  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);
  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');
  52. return 0;/* An indication that the program runs successfully*/
  53. } /* The end of the main method*/
  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. }
  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. }
  70. add_word_in_dictionary (dictionary_array, word_to_be_searched); /* Calling the function add_word_in_dictionary() */
  71. return pointer_dictionary_array;
  72. }
  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.


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.


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

For clarification, the words that are not found are added to the array because the text file was opened in read mode. You can change to read and write mode and add the code for appending extra words to the text file. Thanks

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.