本题目要求编写程序统计一行字符中单词的个数。所谓“单词”是指连续不含空格的字符串,各单词之间用空格分隔,空格数可以是多个。
输入格式:
输入给出一行字符。
输出格式:
在一行中输出单词个数。
输入样例:
Let’s go to room 209.

输出样例:
5

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<iostream>
using namespace std;
int main(void){
char ch;
int num=0;
int size=0;
bool is=true;
while(1){
scanf("%c",&ch);
//cin>>ch 不行
if(ch=='\n'){
break;
}else if(ch!=' '){
size++;
is=true;

}else if(ch==' '){
if(size>0){
num++;
is=false;
size=0;
}

}

}
if(size>0){
num++;
}
//cout<<num;
printf("%d",num);
}