把開發(fā)過程經(jīng)常用到的一些代碼片段做個珍藏,下面的代碼段是關(guān)于C語言統(tǒng)計終端輸入的行數(shù),單詞數(shù)與字符數(shù)的代碼,應(yīng)該對各位有所用。
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);
}
輸出結(jié)果
hello word
ABCD ni hao
love he her
a
b
5 10 39