罗小白


私信TA

用户名:Timmmmy

访问量:16502

签 名:

隔一年又回来刷题了...

等  级
排  名 141
经  验 7205
参赛次数 0
文章发表 46
年  龄 0
在职情况 学生
学  校
专  业

  自我简介:

有问题可以互相交流,共同提高 欢迎私信,请多指教:)

解题思路:
        用string类即可,最后全替换就for循环一下吧~
注意事项:

参考代码:

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int pos;
    char a, b;
    string s, cmd;
    getline(cin, s);
    cin >> cmd;
    if (cmd == "D") {
        cin >> a;
        pos = s.find(a);
        if (pos == string::npos) {
            cout << "NOT Found." << endl;
        }
        else {
            s.erase(pos, 1);
            cout << s << endl;
        }
    }
    else if (cmd == "I") {
        cin >> a >> b;
        pos = s.rfind(a);
        if (pos == string::npos) {
            cout << "NOT Found." << endl;
        }
        else {
            s.insert(pos, 1, b);
            cout << s << endl;
        }
    }
    else if (cmd == "R") {
        cin >> a >> b;
        pos = s.find(a);
        if (pos == string::npos) {
            cout << "NOT Found." << endl;
        }
        else {
            for (int i = pos; i < s.length(); ++i) {
                if (s[i] == a) s[i] = b;
            }
            cout << s << endl;
        }
    }
    return 0;
}


 

0.0分

0 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区