C a Day 2: A minimal wc

Too tired, just the code for now.

  1 /* Program to count the number
  2  * of words in an input character
  3  * sequence. A minimal wc.
  4  */
  5 
  6 #include "stdio.h"
  7 
  8 #define IN 1
  9 #define OUT 0
 10 
 11 int main(void) {
 12     int c, wc, cc, lc, state;
 13 
 14     wc = cc = lc = 0;
 15     state = OUT;
 16 
 17     while ((c = getchar()) != EOF) {
 18         ++cc;
 19 
 20         if (c == '\n')
 21             ++lc;
 22         if (c == ' ' || c == '\t' || c == '\n')
 23             state = OUT;
 24         else if (state == OUT) {
 25             state = IN;
 26             ++wc;
 27         }
 28     }
 29     printf("wc\tcc\tlc\n");
 30     printf("%d\t%d\t%d\n", wc, cc, lc);
 31 }

Compile: cc -o wc word_count.c

and run: ./wc.

Sample output:

hello world
wc    cc    lc
2    12    1