描述
从键盘输入一行字符,统计其中单词的个数,各单词以空格分隔,且空格数可以是多个。
输入
输入只有一行句子。仅有空格和英文字母构成。
输出
单词的个数。
样例输入
stable marriage problem Consists of Matching members
样例输出
7
#include<iostream>
#include<string>
using namespace std;
bool isletter(char s){
if((s>='a' && s<='z') || (s>='A' && s<='Z'))
return 1;
return 0;
}
int main(){
string sentence;
char frontchar;
int i,len,words;
while(getline(cin,sentence)){
len=sentence.length();
words=0;
frontchar='\0';
if(isletter(sentence[0]))
words++;
for(i=0;i<len;i++){
if(isletter(sentence[i]) && frontchar==' ')
words++;
frontchar=sentence[i];
}
cout<<words<<endl;
}
return 0;
}