I am trying to understand a program that counts words! It has an explanation in the book, but it is lame! I do not understand why they have so many variables and why do they need the OUT and IN symbolic constants! This program is confusing me so much and I can't go on further with the book if I do not understand how this program works!
Maybe I have few syntax errors in here, but do not consider them as I already fixed some that I know and it works ! My problem is I donot understand HOW this program works and why is it made so difficult, couldn't it be written in a better way??Code:#include <stdio.h> #define IN 1 #define OUT 0 main() { int c, nl, nw, nc, state; state = OUT; nl = nw = nc = 0; while ((c = getchar()) != EOF) { ++nc; if (c == '\n') ++nl; if(c == ' ' || c == '\n' || c == '\t') state = OUT; else if (state == OUT) { state = IN; ++nw; } } printf("%d %d %d\n", nl, nw, nc); }



LinkBack URL
About LinkBacks




