title: How to count how many words in a .txt file? in C
body: <p>This is my first time posting a question.
  So I am working on a homework program and am a little stuck on a couple of things that I hope someone can chime in and help me with. Here is what I need to do in the program:</p>
  
  <ul>
  <li>Your program must read in a file containing sentences with punctuation.</li>
  <li>It will parse the sentences into words and punctuation.</li>
  <li>The words will be entered into a dictionary and the punctuation into a list. Case is ignored when adding words to the dictionary. Remember a dictionary is kept in lexicographic order.</li>
  <li>Each entry in the dictionary and list will have a count of the number of times the word or punctuation appears in the original text.</li>
  <li>After reading the text (a line with a first character of $ terminates the text), print out the dictionary and list with the counts.</li>
  <li>Your program will next read a line formatted like this: word1 &lt; word2</li>
  <li>This means replace word1 with word2 in the text</li>
  </ul>
  
  <p>I have been able to enter the file (hw5-input) and print it in lexicographic order and got rid of the capitals, I even have a word count but can't get it to print on separate lines with word count.. I still also need to swap the words and print out the file again but printing with word count is what I really need help with. Here is what I have so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;ctype.h&gt;
  
  #define PUNCT " \n,\t!:;.-"
  #define MAX_STR_LEN 2048
  
  struct listNode 
  {
      char *word;
      struct listNode *next;
      int wordCount;
  };
  
  struct listNode *newListNode(const char * const);
  void insertWord(struct listNode *,const char * const);
  void deleteList(struct listNode *);
  void printList(struct listNode *);
  
  // Create new struct listNode
  
  struct listNode *newListNode(const char * const s) 
  {
      struct listNode *n =
          (struct listNode*)calloc(1,sizeof(struct listNode));
      n-&gt;word = (char *)calloc(strlen(s)+1,sizeof(*s));
      strcpy(n-&gt;word,s);
      n-&gt;next = NULL;
      n-&gt;wordCount = 1;
      return n;
  }
  
  // Insert words into dictionary in ascending order
  
  void insertWord(struct listNode *head,const char * const s) 
  {
      char *i;
      int x = 0;
      for(i = s; *i != '\0'; i++) {
          *i = (char)tolower(*i);
          x++;
      }
  
      i = i-x;
  
  // Gets rid of duplicate words and counts words
  
      struct listNode *p = head,
          *q = newListNode(i);
  
      while ((p-&gt;next != NULL) &amp;&amp; (strcmp(i,p-&gt;next-&gt;word) &gt; 0)) 
      {
          p = p-&gt;next;
      }
      if(p-&gt;next != NULL &amp;&amp; strcmp(i,p-&gt;next-&gt;word) == 0)
      {
          p-&gt;next-&gt;wordCount++;
      } else {
          q-&gt;next = p-&gt;next;
          p-&gt;next = q;
      }
  }
  
  // Free all memory allocated for the list
  
  void deleteList(struct listNode *head) 
  {
      struct listNode *p = head, *q;
      while (p != NULL) 
      {
          q = p-&gt;next;
          free(p-&gt;word);
          free(p);
          p = q;
      }
  }
  
  // Print the dictionary
  
  void printList(struct listNode *head) 
  {
      struct listNode *p = head-&gt;next;
  
      while (p != NULL) 
      {
          printf("%s ",p-&gt;word);
          p = p-&gt;next;
      }
      puts("");
  }
  
  // Enter file and print words in lexicographic order
  
  int main(int argc, char *argv[]) 
  {
      char line[MAX_STR_LEN], *s, fileName[MAX_STR_LEN];
      struct listNode *head = newListNode("");
  
      int i = 0;
      char c;
  
      FILE *p;
  
      printf("Enter file name: ");
      scanf("%s", fileName);
          if((p = fopen(fileName, "r")) == NULL)
          {
              printf("File not found.");
              return 0;
          }
  
      while((c = getc(p)) != '$')
      { 
          line[i] = c;
          i++;
      }
  
      line[i] = '\0';
      for(s = strtok(line,PUNCT); s != NULL; s = strtok(NULL,PUNCT)) 
      {
          insertWord(head,s);
      }
      printf("Lexicographical order: ");
      printList(head);
      deleteList(head);
  
      return 0;
  }
  </code></pre>
  
  <p>And the input file (hw5-input) is:</p>
  
  <pre><code>Call me Ishmael.  Some years ago--never mind how long precisely--
  having little or no money in my purse, and nothing particular
  to interest me on shore, I thought I would sail about a little
  and see the watery part of the world.  It is a way I have
  of driving off the spleen and regulating the circulation.
  Whenever I find myself growing grim about the mouth;
  whenever it is a damp, drizzly November in my soul; whenever I
  find myself involuntarily pausing before coffin warehouses,
  and bringing up the rear of every funeral I meet;
  and especially whenever my hypos get such an upper hand of me,
  that it requires a strong moral principle to prevent me from
  deliberately stepping into the street, and methodically knocking
  people's hats off--then, I account it high time to get to sea
  as soon as I can.  This is my substitute for pistol and ball.
  With a philosophical flourish Cato throws himself upon his sword;
  I quietly take to the ship.  There is nothing surprising in this.
  If they but knew it, almost all men in their degree, some time
  or other, cherish very nearly the same feelings towards
  the ocean with me.
  $
  substitute &lt; replacement
  whale &lt; zebra
  myself &lt; oneself
  </code></pre>
  
  <p>I need it to print alphabeticly on next line like this:</p>
  
  <pre><code>a - 4
  about - 1
  account - 1
  ago- 2
  and - 5
  etc..
  </code></pre>