title: Counting number of words with multiple whitespaces
body: <p>I am trying to write a program that prints the number of words found in a text file. Words are defined as sequences of characters separated by any number of white space. </p>
  
  <p>However, I am having a problem when there are multiple whitespaces because then it doesn't report the right number of words. </p>
  
  <p>Here is my code so far:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  
  int main()
  {
     FILE *fp;
     char str;
     int i=0;
  
     /* opening file for reading */
     fp = fopen("myfile.txt" , "r");
     if(fp == NULL) {
        perror("Error opening file");
        return(-1);
     }
     while(( str = fgetc(fp)) != EOF ) {
       if (str == ' ')
               ++i;
     }
  
     printf("%d\n", i);
     fclose(fp);
  
     return(0);
  }
  </code></pre>
  
  <p>myfile.txt is:</p>
  
  <pre><code>Let's do this!      You can do it. Believe in yourself.
  </code></pre>
  
  <p>I'm not sure if I use fgets, fscanf, or fgetc. </p>
  
  <p><strong>Let's say I define  whitespace as it is defined in the fscanf function when reading a string</strong></p>
  
  <p>It prints 14 which is not right. I'm not sure how to account for multiple whitespaces. In this case, whitespaces are any number of spaces between words.  </p>