Section 1.5.4 Word Counting

Program

#include <stdio.h>

#define IN 1  /* inside a word */
#define OUT 0 /* outside a word */

/* count lines, words, and characters in input */
int 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);
}

Explanation

We need to count the number of characters, the number of words and the newlines in the program. We store the characters in a variable c, it’s count in nc, the count of newlines in nl and the number of words in nw. In order to count the words, the trick is to know when we enter the word and when we exit the word. This is kept track by a state variable.

We start with OUTSIDE a word, if we hit a whitespace (’ ‘, t or n), we say, we are outside the word (state = OUT). When we read a character again which is not a whitespace and if were in OUT state earlier, we move to IN state (that is we saw a new word) and we increment nw. For every character we read, we increment nc and for every n we read, we increment nl. The program in the end prints, the nl, nw and nc.