#include using namespace std; int main(){ string s; getline(cin,s); //输入整行字符串,包括空格 int cnt=0; //计数器 for(int i=0;i<s.length();i++){ if(s[i]==' '&&i!=s.length()-1) cnt++; //如果当前字符是空格且不是最后一个字符,则单词数加1 } cout<<cnt+1<<endl; //最后一个单词没有空格,所以要加上1 return 0; }

c++中在1个包含空格和若干单词的英文长句最多100字符中统计单词的个数。输入输入1个长句。输出输出单词的个数。

原文地址: https://www.cveoy.top/t/topic/eowi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录