Word count program in c

93 views Asked by At
#include <stdio.h>

#define IN 1
#define OUT 0

int main()
{

   int nl,nc,nw,state,c;
   nl=nw=nc=0;
   state=OUT;

   while( (c=getchar()) != EOF )
   {
      ++nc;

      if(c=='\n')
          ++nl;

      if(c==' '||c=='\t'||c=='\n')
      {
          state=OUT;
      }
      else if(state==OUT)
      {
        state=IN;
        ++nw;
      }
    }
}

printf("%d\n%d\n%d\n",nw,nl,nc);
}

I entered 1ctrl+z(enter)

ctrl+z(enter)

input and output of my program

Please help me to explain why it didn't work for the first EOF i.e. when it first encountered ctrl+z. And how can one explain nl, nw and nc for this input? Thnx!

0

There are 0 answers