Thanks for taking out time & helping me out guys! Those last few lines in the code are still bothering me. In the meanwhile, I used a twisted logic (no. of words = blank spaces + 1) to create a much simpler program.
Code:
#include <stdio.h>
#include <stdlib.h>
/* #define IN 1 inside a word */
/* #define OUT 0 outside a word */
/* count lines, words, and characters in input */
int main(void) {
int c, nl, nw, nc, nb, state;
/*state = OUT;*/
nl = nw = nc = nb = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == '\n')
++nl;
if (c == ' ')
++nb;
/*if (c == ' ' || c == '\n' || c == '\t')
state = OUT;
else if (state == OUT)
{
state = IN;
++nw;
}*/
}
printf("line:%d word:%d Character:%d\n", nl, nb+1, nc);
return(0);
}
Its know it is silly and will fail if someone presses space-bar twice (among other scenarios) but at least it works in 'normal' circumstances.