統計輸入的行數,單詞數和字符數
#include<stdio.h> #define IN 1 /*在單詞內*/ #define OUT 0 /*在單詞外*/ //統計輸入的行數,單詞數和字符數 int main(){ int c, nl, nw, nc, state; nw=nl=nc=0; state=OUT; while((c=getchar())!=EOF){ nc++; if(c=='\n') nl++; if(c=='\n' || c=='\t' || c==' '){ state=OUT; } else if(state==OUT){ state=IN; nw++; } } printf("%d %d %d\n",nl,nw,nc); getchar(); getchar(); return 0; }
實驗結果