输出一行字符中单词的个数
/*(文件注释头部开始)
*文件名称:字符串中单词个数的统计
* 输入描述:输入一行字符串
* 问题描述: 每两个单词之间以空格(可能多个)隔开,
*或由标点符号(只考虑,.;?!5种)隔开
当输入 I am come from shandong时
* 程序输出:输出单词的个数*/
#include<iostream>
#include<string>
using namespace std;
int main(){
int i = 0, num = 0;
bool isword = false;
string str;
getline(cin, str);
int n = str.size();
while(n--){
if(str[i] == ' '){
isword = false;
}
else if(isword == false){
isword = true;
++num;
}
++i;
}
cout << num << endl;
return 0;
}