文章
44
粉丝
18
获赞
334
访问
25.6k
#include <iostream>
#include <vector>
#include <sstream>
using namespace std;
int main() {
string sentence;
getline(cin, sentence); // 读取整行输入
vector<string> words;
string word;
// 将输入的句子按照空格分割为单词
for (int i = 0; i < sentence.size(); i++) {
if (sentence[i] != ' ') {
word += sentence[i]; // 逐个字符添加到当前单词中
} else if (!word.empty()) {
words.push_back(word); // 遇到空格就把当前单词加入到 words 中
word.clear(); // 清空当前单词
}
}
if (!word.empty()) { // 最后一个单词需要单独处理
words.push_back(word);
}
&...
登录后发布评论
暂无评论,来抢沙发