title: Hash Table in C (find the frequency of every word)
body: <p>I want to create a hash table for an exercise I have to send in my University.
  The program will open a number of files, break each file's content to <code>&lt;&lt;words&gt;&gt;</code> (tokens) and it will save each <code>&lt;&lt;word&gt;&gt;</code> in a hash table with the frequency of each <code>&lt;&lt;word&gt;&gt;</code>. </p>
  
  <p>In case the word is already in the hash table , the program will increase the word's frequency.</p>
  
  <p>At the end the program will print the words and it's frequencies accordingly.
  Also the frequencies should be printed from the highest word frequency to the lowest.
  The comparison of the <code>&lt;&lt;words&gt;&gt;</code> will ignore upper and lower case letters. </p>
  
  <p>For example if a file contains : <code>one two three four Two Three Four THREE FOUR FoUr</code>
  It should print:</p>
  
  <blockquote>
    <p>four 4<br>
    three 3<br>
    two 2<br>
    one 1</p>
  </blockquote>
  
  <p>The professor gave us a template that we should complete but I'm really confused on what to do with the <code>insert_ht()</code> and <code>clear_ht()</code> functions as well as the compare one. </p>
  
  <p>Here is the code :</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;ctype.h&gt;
  
  #define HTABLE_SIZ 1001
  #define MAX_LINE_SIZ 1024
  
  /* Hash Table */
  typedef struct node* link;
  struct node { char *token; int freq; link next; };
  
  link htable[HTABLE_SIZ] = { NULL }; /* Table of lists (#buckets) */
  int size = 0; /* Size (number of elements) of hash table */
  
  unsigned int hash (char *tok );
  void insert_ht (char *data);
  void clear_ht ( );
  void print_ht ( );
  
  void Process(FILE *fp);
  
  
  int main(int argc, char *argv[])
  {
      int i;
      FILE *fp;
      for (i=1; i &lt; argc; i++)
      {
          fp = fopen(argv[i],"r");
          if (NULL == fp)
          {
              fprintf(stderr,"Problem opening file: %s\n",argv[i]);
              continue;
          }
      Process(fp);
      fclose(fp);
      }
      print_ht();
      clear_ht();
      return 0;
  }
  
  
  void Process(FILE *fp)
  {
      const char *seperators = " ?!'\";,.:+-*&amp;%(){}[]&lt;&gt;\\\t\n";
  
      char line[MAX_LINE_SIZ];
      char *s;
      while((fgets(line,MAX_LINE_SIZ, fp)) != NULL)
      {
          for (s=strtok(line,seperators); s; s=strtok(NULL,seperators))
              insert_ht(s);
          }
      }
  
  /* Hash Function */
  unsigned int hash(char *tok)
  {
      unsigned int hv = 0;
      while (*tok)
          hv = (hv &lt;&lt; 4) | toupper(*tok++);
      return hv % HTABLE_SIZ;
  }
  
  
  void insert_ht(char *token)
  {
  ……………………………………………
  }
  void clear_ht()
  {
  ……………………………………………
  }
  int compare(const void *elem1, const void *elem2)
  {
  ……………………………………………
  }
  void print_ht()
  {
      int i, j=0;
      link l, *vector = (link*) malloc(sizeof(link)*size);
      for (i=0; i &lt; HTABLE_SIZ; i++)
          for (l=htable[i]; l; l=l-&gt;next)
              vector[j++] = l;
          qsort(vector,size,sizeof(link),compare);
          for (i=0; i &lt; size; i++)
              printf("%-50s\t%7d\n",vector[i]-&gt;token,vector[i]-&gt;freq);
          free(vector);
  }
  </code></pre>