题目描述输入一行字符串字符串长度 ≤100≤100 包含若干个单词约定相邻的两个单词用空格隔开一个或多个空格编程统计单词的个数。输入一行空格隔开的若干个单词。输出单词个数。样例输入复制Hello World输出复制2c++ 代码
#include <iostream>
using namespace std;
int main() {
string s;
getline(cin, s);
int count = 0;
int n = s.length();
bool wordStart = false;
for (int i = 0; i < n; i++) {
if (s[i] != ' ') {
if (!wordStart) {
count++;
wordStart = true;
}
} else {
wordStart = false;
}
}
cout << count << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/hRUD 著作权归作者所有。请勿转载和采集!