title: Spell checker program, check function speller cs50
body: <p>This program works as a spell checker, it reads a dictionary file to load into the hash table, then reads another text file that will be read and will check every word if it is in the hash table, if not then it is considered a misspelled word. All of my functions seem to work except for my check function, when I run it the number of misspelled words is always the same as the number of words in text. This was working before but I changed the hash function because this hash function was said to be better to assign the values into unique index, but after changing just the hash function the check function doesn't work anymore.</p>
  <pre><code>// Implements a dictionary's functionality
  #include &lt;stdbool.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  #include &lt;string.h&gt;
  
  #include &quot;dictionary.h&quot;
  
  // Represents a node in a hash table
  typedef struct node
  {
      char word[LENGTH + 1];
      struct node *next;
  }
  node;
  
  // Number of buckets in hash table
  const unsigned int N = 200000;
  
  // Hash table
  node *table[N];
  
  // Returns true if word is in dictionary else false
  bool check(const char *word)
  {
      // TODO
      int len = strlen(word);
      char copy[len + 1];
      // change into lowercase the word
      for (int i = 0; i != '\0'; i++)
      {
          copy[i] = tolower(word[i]);
      }
      // get the index by using the hash function
      int index = hash(copy);
      if (table[index] == NULL)
      {
          return false;
      }
  
      node *tmp = table[index];
      // check if the word is in the hash table
      while (tmp != NULL)
      {
          if (strcmp(tmp-&gt;word, copy) == 0)
          {
              return true;
          }
  
          tmp = tmp-&gt;next;
      }
  
      return false;
  }
  
  // Hashes word to a number
  unsigned int hash(const char *word)
  {
      /* credits to...
       *https://www.reddit.com/r/cs50/comments/1x6vc8/pset6_trie_vs_hashtable/
       */
      unsigned int hash = 0;
      for (int i = 0, n = strlen(word); i &lt; n; i++)
      {
          hash = (hash &lt;&lt; 2) ^ word[i];
      }
      return hash % N;
  }
  
  // Loads dictionary into memory, returning true if successful else false
  bool load(const char *dictionary)
  {
      // TODO
      char *words = malloc(sizeof(char) * (LENGTH + 1));
      if (words == NULL)
      {
          return 1;
      }
      // initialize the hash table to NULL
      for (int i = 0; i &lt; N; i++)
      {
          table[i] = NULL;
      }
  
      // open dictionary file
      FILE *indata = fopen(dictionary, &quot;r&quot;);
  
      // 1 character for '\0' and another for '\n' because fgets takes a trailing new line
      // when it reads 'man' the value of words will be &quot;man\n\0&quot; so meaning 2 extra characters
      while (fgets(words, LENGTH + 2, indata) != NULL)
      {
          // get the index by using the hash function
          int index = hash(words);
          // allocate memory for the newNode
          node *newNode = malloc(sizeof(node));
          if (newNode == NULL)
          {
              return false;
          }
  
          // get rid of the trailing new line from fgets
          words[strlen(words) - 1] = '\0';
          strcpy(newNode-&gt;word, words);
          // make the newNode the head of the list
          newNode-&gt;next = table[index];
          table[index] = newNode;
      }
  
      // free memory and close the opened file
      free(words);
      fclose(indata);
      return true;
  }
  
  // Returns number of words in dictionary if loaded else 0 if not yet loaded
  unsigned int size(void)
  {
      // TODO
      // counter of words loaded
      unsigned int counter = 0;
      // loop through the hash table
      for (int i = 0; i &lt; N; i++)
      {
          node *tmp = table[i];
  
          while (tmp != NULL)
          {
              counter++;
              tmp = tmp-&gt;next;
          }
      }
      return counter;
  }
  
  // Unloads dictionary from memory, returning true if successful else false
  bool unload(void)
  {
      // TODO
      // loop through the whole hash table
      for (int i = 0; i &lt; N; i++)
      {
          while (table[i] != NULL)
          {
              node *tmp = table[i]-&gt;next;
              free(table[i]);
              table[i] = tmp;
          }
      }
      return true;
  }
  </code></pre>