//#include "stdafx.h"//If the vc++6.0, with this line.
#include <string>
#include <iostream>
using namespace std;
int main(void){
string str,s;
char ch;
int t,f=0;
cout << "Please enter a string...\nstr=";
cin >> str;
cout << "Please enter the characters to be replaced...\nch=";
cin >> ch;
while(1){
cout << "Please enter a string to replace...\ns=";
if(cin >> s && s.find(ch)==string::npos)
break;
cout << "Error(there a '" << ch << "' in s, redo: ";
}
while((t=str.find(ch))!=string::npos){
str.replace(t,1,s);
f++;
}
if(f)
cout << "Replace " << f << " '" << ch << '\'' << endl;
else
cout << "Could not find the '" << ch << '\'' << endl;
cout << str << endl;
return 0;
}
温馨提示:答案为网友推荐,仅供参考