题解 | #单词替换#

单词替换

https://www.nowcoder.com/practice/5b58a04679d5419caf62c2b238e5c9c7

#include<iostream>
#include<string>
#include<vector>

using namespace std;
int main() {
    string sentence, target_str, replace_str;
    getline(cin, sentence);
    getline(cin, target_str);
    getline(cin, replace_str);
    sentence = sentence + " ";
    vector<string>words;
    string temp_str = "";
    for (int i = 0; i < (int)sentence.size(); i++) {
        if (sentence[i] != ' ') {
            temp_str = temp_str + sentence[i];
        } else {
            words.push_back(temp_str);
            temp_str = "";
        }
    }
    for (auto iter = words.begin(); iter != words.end(); iter++) {
        if ((*iter) != target_str) {
            cout << *iter << " ";
        } else {
            cout << replace_str << " ";
        }


    }


}

全部评论

相关推荐

05-26 10:24
门头沟学院 Java
qq乃乃好喝到咩噗茶:其实是对的,线上面试容易被人当野怪刷了
找工作时遇到的神仙HR
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务