Advertisement
rupek1995

Untitled

Mar 31st, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.05 KB | None | 0 0
  1. /**
  2.  * Returns true if word is in dictionary else false.
  3.  */
  4. bool check(const char *word)
  5. {
  6.     // copy const char to new variable (to lowercase it)
  7.     char *low_case_word = malloc((strlen(word) + 1) * sizeof(char));
  8.     strcpy(low_case_word, word);
  9.  
  10.     // convert word to lowercase
  11.     for(int i = 0; i < strlen(word); i++)
  12.     {
  13.         low_case_word[i] = tolower(word[i]);
  14.     }
  15.     //check hash index of current word
  16.     int hash_index = hash(low_case_word);
  17.  
  18.     return check_single_index(hashtabe_array[hash_index], low_case_word);
  19. }
  20.  
  21.  
  22. bool check_single_index(hash_table *table, char *low_case_word)
  23. {
  24.     // create a temporary pointer for traveling across nodes
  25.     hash_table *trav = table;
  26.  
  27.     // if word is not first in dictionary - go further
  28.     while(trav != NULL)
  29.     {
  30.         // if current node includes word - return true
  31.         if(strcmp(trav->word, low_case_word) == 0)
  32.         {
  33.             free(low_case_word);
  34.             return true;
  35.         }
  36.  
  37.         // go to next node
  38.         trav = trav->next;     
  39.     }
  40.    
  41.     free(low_case_word);    
  42.     return false;
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement