题解 | 单词替换

单词替换

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

#include <iostream>
#include <cstring>
#include <sstream>

using namespace std;

const int N = 100 + 10;

stringstream ss;

string s;
string a, b;
string word[N];
string ans;

int main() {
    while (getline(cin, s)) {
        getline(cin, a);
        getline(cin, b);

        ss.str(s);
        int n = 0;
        while (ss >> word[n]) n++;

        for (int i = 0; i < n; i++) {
            if (word[i] == a) ans += b + ' ';
            else ans += word[i] + ' ';
        }

        cout << ans << endl;
    }  
}

stringstream把单词拆分到数组中,然后逐个加到答案即可。

全部评论

相关推荐

11-06 16:50
门头沟学院 Java
用微笑面对困难:word打字比赛二等奖的我,也要来凑合凑合
点赞 评论 收藏
分享
12-27 22:49
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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