title: Printing the most frequent occurring words in a given text file, unable to sort by frequency in C
body: <p>I am working on an assignment that requires me to print the top 10 most occurring words in a given text file. My code is printing the words from the file, but it is not sorting them according to their frequency. </p>
  
  <p>Here is come of my code below. I use a hashtable to store each unique word and its frequency. I am currently sorting the words using the wordcmp function I wrote and calling it in the inbuilt qsort function in main. </p>
  
  <p>If anyone can guide me to fix my error, I'd be very greatful. </p>
  
  <p>My current output:</p>
  
  <p>the top 10 words (out of 10) are:</p>
  
  <p>1 im</p>
  
  <p>1 are</p>
  
  <p>1 again</p>
  
  <p>3 happy</p>
  
  <p>2 hello</p>
  
  <p>1 how</p>
  
  <p>1 lets</p>
  
  <p>1 you</p>
  
  <p>1 try</p>
  
  <p>1 this</p>
  
  <p>Expected output (what I want):</p>
  
  <p>The top 10 words (out of 10) are:</p>
  
  <p>3 happy</p>
  
  <p>2 hello</p>
  
  <p>1 you</p>
  
  <p>1 try</p>
  
  <p>1 this</p>
  
  <p>1 lets</p>
  
  <p>1 im</p>
  
  <p>1 how</p>
  
  <p>1 are</p>
  
  <p>1 again</p>
  
  <p>Here is some of my code: </p>
  
  <pre><code>typedef struct word
  { 
    char *s;          /* the word */
    int count;        /* number of times word occurs */
    struct word* next;
  }word;
  
  struct hashtable
  {
    word **table;
    int tablesize;
    int currentsize;
  };
  typedef struct hashtable hashtable;
  int main(int argc, char *argv[])
  {
  
      int top_words = 10;
      word *word = NULL;
      hashtable *hash = ht_create(5000);
      char *file_name;
      char *file_word;
      FILE *fp;
      struct word *present = NULL;
  
      fp = fopen (file_name, "r");
      if (fp == NULL)
      {
          fprintf (stderr,"%s: No such file or directory\n", file_name);
          fprintf(stderr,"The top %d words (out of 0) are:\n", top_words); 
          exit(-1);
      }
  
      continue_program:
      while ((file_word = getWord(fp)))
      {
          word = add(hash, file_word, 1);
      }
      fclose(fp);
  
      qsort((void*)hash-&gt;table, hash-&gt;currentsize, sizeof(word),(int (*)(const void *, const void *)) wordcmp);
  
      if(top_words &gt; total_unique_words)
            top_words = total_unique_words;
  
      printf("the top %d words (out of %d) are:\n", top_words, total_unique_words);
  
      int iterations =0;
      for(i =0; i &lt;= hash-&gt;tablesize &amp;&amp; iterations&lt; top_words; i++)
      {
            present = hash-&gt;table[i];
            if(present != NULL)
            {
                printf("     %4d %s\n", present-&gt;count, present-&gt;s);
                present = present-&gt;next;
                iterations++;
            }
      }
      freetable(hash);
  
   return 0;
  }
  
  int wordcmp (word *a, word *b) 
  {
      if (a != NULL &amp;&amp; b!= NULL) {
  
      if (a-&gt;count &lt; b-&gt;count) 
      {
        return +1;     
      }
      else if (a-&gt;count &gt; b-&gt;count) 
      {
          return -1; 
      }
      else if (a-&gt;count == b-&gt;count)
      {
        /*return strcmp(b-&gt;s, a-&gt;s);*/
        return 0;
      }
    }
    return 0;
  }
  
  /* Create a new hashtable. */
  struct hashtable *ht_create( int size ) 
  {
    int i;
  
    if( size &lt; 1 ) 
      return NULL;
  
    hashtable *table = (hashtable *) malloc(sizeof(hashtable));
    table-&gt;table = (word **) malloc(sizeof(word *) * size);
  
    if(table != NULL)
    {
        table-&gt;currentsize = 0;
        table-&gt;tablesize = size;
    }
  
    for( i = 0; i &lt; size; i++ ) 
    {
      table-&gt;table[i] = NULL;
    }
  
    return table; 
  }
  
  /* Adds a new node to the hash table*/
  word * add(hashtable *h, char *key, int freq) 
  {
      int index = hashcode(key) % h-&gt;tablesize;
      word *current = h-&gt;table[index];
  
      /* Search for duplicate value */
      while(current != NULL) {
          if(contains(h, key) == 1){
              current-&gt;count++;
              return current;
         }
           current = current-&gt;next;
       }
  
      /* Create new node if no duplicate is found */
      word *newnode = (struct word*)malloc(sizeof(struct word));
      if(newnode!=NULL){
            newnode-&gt;s =strdup(key);
            newnode-&gt; count = freq;
            newnode-&gt; next = NULL;
      }
      h-&gt;table[index] = newnode;
      h-&gt;currentsize = h-&gt;currentsize + 1;
      total_unique_words++;
      return newnode;
  }
  </code></pre>