把开发过程经常用到的一些代码片段做个珍藏,下面的代码段是关于C语言统计终端输入的行数,单词数与字符数的代码,应该对各位有所用。
include <stdio.h>
main() {
int c, nl, nw, nc, state;
state = OUT;
nl = nw = nc = 0;
while ((c = getchar()) != EOF) {
++nc;
if (c == 'n') {
++nl;
}
if (c == ' ' || c == 'n' || c == 't') {
state = OUT;
} else if (state == OUT) {
state = IN;
++nw;
}
}
printf("%d %d %d", nl, nw, nc);
}
输出结果
hello word
ABCD ni hao
love he her
a
b
5 10 39