Advertisement
rupek1995

hashtable

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