counts number of lines, words, and characters in input: - C String

C examples for String:String Console Input

Description

counts number of lines, words, and characters in input:

define IN  1                                  /* inside a word */
define OUT 0                                  /* outside a word */
int ch, lines, words, chars, state;
state = OUT;
lines = words = chars = 0;
while ((ch = getchar()) != EOF) {
  ++chars;
  if (ch == '\n')
    ++lines;
  if (ch == ' ' || ch == '\n' || ch == '\t')
     state = OUT;
  else if (state == OUT) {
     state = IN;
     ++words;
  }
}
printf("%d %d %d\n", lines, words, chars);

Related Tutorials