title: Length of words from getchar()
body: <p>I'm trying to write a program in C to print the length of words entered through getchar(). Here's the code :</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  
  #define IN 1
  #define OUT 0
  
  int main()
  {
  int c, chars, state, i;
  int nlength[20];
  
  state = OUT;
  chars = 0;
  
  for (i = 0; i &lt; 20; ++i){
     nlength[i] = 0;
  }
  
  
  while ((c = getchar()) != EOF){
       if (c != ' ' &amp;&amp; c != '\t' &amp;&amp; c != '\n'){
          state = IN;
          ++chars;
       }
  
       else if (state == OUT){
               ++nlength[chars];
               chars = 0;
       }
  }
  if (c == EOF){
    ++nlength[chars];
  }  
  
  printf("\nLength of words = ");
  
  for (i = 0; i &lt; 20; ++i){
     printf(" %d", nlength[i]);
  }
  
  printf("\n");
  }
  </code></pre>
  
  <p>It's supposed to output, for an example of an input of "aaa aaa", : 0 0 0 2 0 0 0 ... .
  However, it outputs something like 0 0 0 0 0 0 1 0 0... . Can anyone tell me what's wrong with it?</p>