c-first-steps

a C playground
Index Commits Files Refs README
   1 /*    count lines, words, and characters in input    */
   2 
   3 
   4 #include <stdio.h>
   5 
   6 #define    IN    1    /* inside a word    */
   7 #define    OUT    0    /* outside a word    */
   8 
   9 int main(void) {
  10 
  11     int c, nl, nw, nc, state;
  12 
  13     state = OUT;
  14     nl = nw = nc = 0;
  15 
  16     while((c = getchar()) != EOF) {
  17         if(c != '\n') {
  18             ++nc;
  19         }    else {
  20             ++nl;
  21         }
  22 
  23         if((c == ' ') || (c == '\n') || (c == '\t')) {
  24             state = OUT;
  25         }    else if(state == OUT) {
  26             state = IN;
  27             ++nw;
  28         }
  29 
  30 
  31     }
  32 
  33     printf("lines: %d, words: %d, chars: %d\n", nl, nw, nc);
  34 
  35     return 0;
  36 }