geneviedube

Untitled

Sep 3rd, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.54 KB | None | 0 0
  1. bool load(const char *dictionary)
  2. {
  3.     //no need to consider the case senstive
  4.  
  5.     //create memory space for the first node (lenght depend of the word)
  6.     root = malloc(sizeof(node));
  7.  
  8.     // note lenght min for a word is 2 letters (+1) and the max is 45 ( the longest word)
  9.     // open the dictionnary file to read his content
  10.     FILE *fp = fopen(dictionary, "r");
  11.     if (fp == NULL)
  12.     {
  13.         printf("Could not open %s.\n", dictionary);
  14.         return false;
  15.     }
  16.  
  17.      //get dictionaryfile
  18.     for (int c = fgetc(fp); c!= EOF; c = fgetc(fp))
  19.     {
  20.  
  21.         if (isalpha(c))
  22.         {
  23.             letter_position = (c - 65);
  24.         }
  25.  
  26.         else if(c == '\'')
  27.         {
  28.             letter_position = 26;
  29.         }
  30.  
  31.         //start with the root to check
  32.         if(root->children[letter_position] == NULL)
  33.         {
  34.             //make sure that next word start with the root
  35.             temp_pointer = root->children[letter_position];
  36.         }
  37.  
  38.  
  39.         else
  40.         {
  41.             //check the next node
  42.             temp_pointer = malloc(sizeof(node));
  43.             // check if next node exist if not NULL
  44.             if(temp_pointer->children[letter_position]) == NULL)
  45.  
  46.  
  47.             else
  48.             {
  49.                 //move to the next node
  50.                 temp_pointer = temp_pointer->children[letter_position];
  51.  
  52.             }
  53.  
  54.  
  55.             else
  56.             {
  57.                 temp_pointer->is_word = true;
  58.                 return true;
  59.             }
  60.  
  61.         }
  62.  
  63.     }
  64.  
  65. fclose(fp);
  66. return true;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment