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. /*Implements a dictionary's functionality.*/
  2. #include <stdbool.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <ctype.h>
  6. #include "dictionary.h"
  7. // root node of trie, pointing to nothing
  8. node* root = NULL;
  9. // word counter, keep track of read words, set to zero
  10. int word_count = 0;
  11. /**
  12.  * Returns true if word is in dictionary else false.
  13.  */
  14. bool check(const char* word)
  15. {
  16.     // iterate through word char-by-char
  17.     for (int index = 0, n = strlen(word); index < n; index++)
  18.     {
  19.         // conver each letter of word into lower case
  20.         char c = tolower(word[index]);
  21.        
  22.         // to store index of children array
  23.         int i = 0;
  24.         if (== '\'')
  25.         {
  26.             // is apostrophy (') detected, give it last index of the array
  27.             // i.e. size of array - 1
  28.             i = SIZE - 1;
  29.         }
  30.        
  31.         else
  32.         {
  33.             // convert letter into 0-25 from a-z
  34.             i = c - 'a';
  35.         }
  36.        
  37.         // create a tracker pointer to go through trie to check
  38.         node* tracker = root;
  39.        
  40.         if (tracker->children[i] == NULL)
  41.         {
  42.             // ith letter of word is not poiting to anything
  43.             // means word is not in dictionary
  44.             return false;
  45.         }
  46.        
  47.         else if (tracker->children[i] != NULL)
  48.         {
  49.             // poiting to something, so move to the next node
  50.             tracker = tracker->children[i];
  51.         }
  52.         // end of the word from text
  53.         if (index == n - 1)
  54.         {
  55.             return true;
  56.         }
  57.     }
  58.     return false;
  59. }
  60. /**
  61.  * Loads dictionary into memory.  Returns true if successful else false.
  62.  */
  63. bool load(const char* dictionary)
  64. {
  65.     // open dictionary in read only mode, sanity check
  66.     FILE* inptr = fopen(dictionary, "r");
  67.     if (inptr == NULL)
  68.     {
  69.         printf("Error! Couldn't open file.\n");
  70.         return false;
  71.     }
  72.    
  73.     // give root node something to point to, do sanity check
  74.     root = malloc(sizeof(node));
  75.     if (root == NULL)
  76.     {
  77.         printf("Error! Out of Memory.\n");
  78.         fclose(inptr);
  79.         return false;
  80.     }
  81.    
  82.     // to traverse through dictionary, tracker pointer poiting to root
  83.     node* tracker = root;
  84.    
  85.     // read dictionary char-by-char to end of file
  86.     for (char c = fgetc(inptr); c != EOF; c = fgetc(inptr))
  87.     {
  88.         // set is_word flag to false
  89.         tracker->is_word = false;
  90.        
  91.         // to store index of children array
  92.         int i = 0;
  93.        
  94.         // to convert char into lowercase, make it easy to index
  95.         c = tolower(c);
  96.        
  97.         // indexing
  98.         if (islower(c))
  99.         {
  100.             // if lower alpha, convert it into 0-25 from a-z
  101.             i = c - 'a';
  102.         }
  103.        
  104.         else if (== '\'')
  105.         {
  106.             // if char is apostrophe (') give it last index of the array
  107.             // i.e. size of array - 1
  108.             i = SIZE - 1;
  109.         }
  110.        
  111.         else if (== '\n')
  112.         {
  113.             // if char is \n, indicator of new word, flag is_word = true
  114.             tracker->is_word = true;
  115.            
  116.             // update word counter
  117.             word_count++;
  118.            
  119.             // reset tracker to root
  120.             tracker = root;
  121.         }
  122.        
  123.         else
  124.         {
  125.             // if any of above char is not detected, error in word reading
  126.             i = -1;
  127.             break;
  128.         }
  129.         // end of the indexing
  130.        
  131.         // use index to follow a path in trie
  132.         if (tracker->children[i] == NULL)
  133.         {
  134.             // tracker isn't pointing to anything
  135.             // create a new node to point to, do sanity check
  136.             node* new = malloc(sizeof(node));
  137.             if (new == NULL)
  138.             {
  139.                 printf("Error! Out of Memory.\n");
  140.                 fclose(inptr);
  141.                 return false;
  142.             }
  143.            
  144.             // make tracker to point to new node
  145.             tracker->children[i] = new;
  146.            
  147.             // move tracker to point to next node
  148.             tracker = tracker->children[i];
  149.            
  150.         }
  151.        
  152.         else if (tracker->children[i] != NULL)
  153.         {
  154.             // pointer tracker is pointing to something
  155.             // move tracker to point to next node
  156.             tracker = tracker->children[i];
  157.         }
  158.     }
  159.    
  160.     // close opened files
  161.     fclose(inptr);
  162.    
  163.     // if everything goes fine
  164.     return true;
  165. }
  166. /**
  167.  * Returns number of words in dictionary if loaded else 0 if not yet loaded.
  168.  */
  169. unsigned int size(void)
  170. {
  171.     // return number if word that are read
  172.     return word_count;
  173. }
  174. /**
  175.  * Unloads dictionary from memory.  Returns true if successful else false.
  176.  */
  177. bool unload(void)
  178. {
  179.     // TODO
  180.     return false;
  181. }


Learn More :