#include <stdio.h> #define IN 1 //在單詞中 #define OUT 0 //不在單詞中 /* The C programming language[second edition] 練習小程序 功能:統計輸入的字符數、單詞數和行數。 */ int main() { int c; //存儲輸入的單詞 int STATE = OUT; //狀態初始為不在單詞中 int nc = 0; //記錄新的字符 int nl = 0; //記錄新的行 int nw = 0; // 記錄新的單詞 while ((c = getchar()) != 'q' ) //鍵入字符'q'則退出輸入 { nc++; if(c == '\n') nl++; if(c == ' ' || c == '\n' || c == '\t') STATE = OUT; else if (STATE == OUT) { STATE = IN; nw++; } } printf("%d % d %d\n",nl,nw,nc); return 0; }