#include <iostream> #include <stdio.h> #include <string> #include <sstream> using namespace std; string& replace_all(string& str, const string& old_value, const string& new_value) { while (true) { string::size_type pos(0); if ((pos = str.find(old_value)) != string::npos) str.replace(pos, old_value.length(), new_value); else break; } return str; } int main() { string begannum; string old_num; string new_num; printf("请输入你的字符串:"); cin >> begannum; printf("请输入原来要替换的字符串:"); cin >> old_num; printf("请输入要替换成的字符串:"); cin >> new_num; cout << replace_all(begannum, old_num, new_num) << endl; while (1); return 0; }
运行结果:
借鉴自:https://www.cnblogs.com/catgatp/p/6407783.html
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
评论