前后加上空格巧妙规避空格困难

单词替换

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

#include <iostream>
using namespace std;

int main() {
    string str, base, replace;//句子 door key
    getline(cin, str);//输入一行
    str = " " + str + " ";//前后加上空格
    getline(cin, base);
    base = " " + base + " ";//前后加上空格
    getline(cin, replace);
    replace = " " + replace + " ";//前后加上空格
    while (str.find(base) != string::npos) {//找得到进入循环
        int found = str.find(base);
        str.erase(found, base.size());
        str.insert(found, replace);
    }
    cout << str.substr(1, str.size() - 2) << endl;//不输出前后空格
    return 0;
}

全部评论

相关推荐

zbk1:学院本找嵌入式我觉得不太行,不要被培训班忽悠了,老老实实读个研吧。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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