统计行数、单词数,字符数的程序:
1 #include2 3 #define IN 1 /*在单词内*/ 4 #define OUT 0 /*在单词外*/ 5 6 int main() 7 { 8 int c, nl, nw, nc, state; 9 10 state = OUT;11 nl = nw = nc = 0; //nl:行数 nw:单词数 nc:字符数12 while((c = getchar()) != EOF) {13 ++nc;14 if(c == '\n')15 ++nl;16 if(c == ' ' || c == '\n' || c == '\t')17 state = OUT;18 else if (state == OUT) {19 state = IN;20 ++nw;21 }22 }23 printf("%d %d %d\n", nl, nw, nc);24 return 0;25 }
【练习1-12】
编写一个程序,以每行一个单词的形式打印其输入
1 #include2 3 #define IN 1 /*在单词内*/ 4 #define OUT 0 /*在单词外*/ 5 6 int main() 7 { 8 int c, state; 9 state = OUT;10 11 while((c =getchar()) != EOF) {12 if(c == ' ' || c == '\n' || c == '\t') {13 if(state == IN) {14 putchar('\n');15 state = OUT;16 }17 } else if(state == OUT) {18 state = IN;19 putchar(c);20 } else {21 putchar(c);22 }23 }24 return 0;25 }