// Count characters, words, and lines in input // Output the words in different lines #include <stdio.h> #define IN 1 // inside a word #define OUT 0 // outside a word int main() { int c, nl, nw, nc, state; nl = nw = nc = 0; state = OUT; // state记录的是前面获取的字符的状态,初始化为OUT while ((c = getchar()) != EOF) { ++nc; // 统计字符数 if (c == '/n') { ++nl; // 统计行数 } if (c == ' ' || c == '/t' || c == '/n') { if (state == IN) { // 单词结束 putchar('/n'); state = OUT; } } else if (state == OUT) { // 单词的首字符 state = IN; ++nw; putchar(c); } else // 输出单词中除过第一个字符的其他字符 { putchar(c); } } printf("nc = %d, nw = %d, nl = %d/n", nc, nw, nl); return 0; }