title: Count words in a user-input string in C
body: <p>So, we were given this program in class. "Write a Program in C to count the number of words in a sentence input by the user."
  This is what i could come up with, but the number of words is always one less than what is the correct number. My teacher told everyone to just add 1 to the word count before printing it. I think it has a bug, if we don't enter any words, i.e. , press Enter instead of typing,the program suggested by my teacher would still give word count as 1 not 0. Do you know of any way to get proper word count without just adding 1 at the end?
  Code:</p>
  
  <p>My code(giving 1 less than correct) :    </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  void main()
  {
   char s[200];
   int count = 0, i;
   printf("enter the string\n");
   gets(s);
   for (i = 0;i&lt;strlen(s);i++)
   {
    if (s[i] == ' ')
    count++;    
   }
   printf("number of words in given string are: %d\n", count);
  }
  </code></pre>