文章
132
粉丝
19
获赞
402
访问
60.7k
 
#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);
    }
&...
登录后发布评论
暂无评论,来抢沙发