7-29 統計一行文本的單詞個數 (15分)
本題目要求編寫程序統計一行字符中單詞的個數。所謂“單詞”是指連續不含空格的字符串,各單詞之間用空格分隔,空格數可以是多個。
輸入格式:
輸入給出一行字符。
輸出格式:
在一行中輸出單詞個數。
輸入樣例:
Let's go to room 209.
輸出樣例:
5
#include<stdio.h> int main() { int inword=0,count=0,flag=0; char page[200],c; while((c=getchar())!='\n') { if(!inword&&c!=' ') { count++; inword=1; } else { if(inword&&c==' ') inword=0; } } printf("%d\n",count); return 0; }
