Showing posts with label Dictionary. Show all posts
Showing posts with label Dictionary. Show all posts

Dictionary Word Search C Program

How to write a C Program to Find Dictionary Word Search ?


Solution For C Program to Find a Word in Dictionary :

C Program to Implements a dictionary's functionality

How to write a C Program to Implements a dictionary's functionality in C Programming Language ?

Solution:

  1. /****************************************************************************
  2.  * dictionary.c
  3.  * Implements a dictionary's functionality.

  4.  ***************************************************************************/
  5. #include <stdbool.h>
  6. #include <stdlib.h>
  7. #include <stdio.h>
  8. #include "dictionary.h"
  9. // declare structure for each word
  10.     typedef struct node
  11.     {
  12.         char word[LENGTH + 1];
  13.         struct node* next;
  14.     }
  15.     node;
  16.    
  17. bool hash(const char* word)
  18. {
  19.     // index < the number of buckets
  20.     // deterministic - the same value needs to map to the same bucket every time
  21. }
  22. /**
  23.  * Returns true if word is in dictionary else false.
  24.  */
  25. bool check(const char* word)
  26. {
  27.     // TODO
  28.     return false;
  29. }
  30. /**
  31.  * Loads dictionary into memory.  Returns true if successful else false.
  32.  */
  33. bool load(const char* dictionary)
  34. {
  35.    
  36.     // TODO
  37.    
  38.    
  39.     // declare file pointer for dictionary file
  40.     FILE* input = NULL;
  41.     // open dictionary
  42.     input = fopen(dictionary, "r");
  43.     if (input == NULL)
  44.     {
  45.         return false;
  46.     }
  47.     // declare memeory space or each word
  48.     node* new_node = malloc(sizeof(node));
  49.     while(input != EOF)
  50.     {
  51.     // scan dictionary after opening for each word before a\n and stores in in new_code->word
  52.     fscanf(input, "%s", new_node->word);
  53.     // hash the word (takes string and returns an index)
  54.     hash(word);
  55.     // this is a example on how to put a word in the front of the list
  56.     new_node->next = head;
  57.     head = new_node;
  58.     }
  59.    
  60.     return false;
  61. }
  62. /**
  63.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  64.  */
  65. unsigned int size(void)
  66. {
  67.     // TODO
  68.     return 0;
  69. }
  70. /**
  71.  * Unloads dictionary from memory.  Returns true if successful else false.
  72.  */
  73. bool unload(void)
  74. {
  75.     // TODO
  76.     return false;
  77. }

C Program to opens and reads dictionary file specified in spellrc

How to write a C Program to opens and reads dictionary file specified in spellrc in C Programming Language ?

Solution:


/*opens and reads dictionary file specified in spellrc*/

 if (strcmp(settings_p->autoCorrect, "yes") == 0) { 
  actionFunc_p = &autoCorrect;
  printf("action function set to autoCorrect\n");
 } else {
  actionFunc_p = &noAutoCorrect;
  printf("action function set to no autoCorrect\n");
 }

 printf("Current value of dictionary: %s\n", settings_p->dictionary);
 
 dictFile_p = fopen(settings_p->dictionary, "r");
  if (!dictFile_p) {
   perror(settings_p->dictionary);
   return 1;
  }

 printf("Opened dictionary file successfully\n");
/*
TERMINAL OUTPUT:
Current value of dictionary: thedictionary.txt
action function set to autoCorrect
Current value of dictionary: #
��� : No such file or directory

*/