fork download
  1. #include<iostream>
  2. #include<string>
  3. using namespace std;
  4.  
  5. string findReplace(string str,string find,string replace){
  6. int pos=0;
  7. while(true){
  8. pos = str.find(find);
  9. if (pos == -1) break;
  10. str.replace(pos, find.length(),replace );
  11.  
  12. }
  13. return str;
  14. }
  15.  
  16. string findErase(string str,string find){
  17. int pos=0;
  18. while(true){
  19. pos = str.find(find);
  20. if (pos == -1) break;
  21. str.erase(pos, find.length());
  22. }
  23. return str;
  24. }
  25.  
  26.  
  27. int main(){
  28. string str,find,replace,erase;
  29. cout<<"enter the string: ";
  30. getline(cin,str);
  31. cout<<"enter the word you want to find: ";
  32. getline(cin,find);
  33. cout<<"do you wish to erase it or to replace it?(erase,replace)";
  34. string wish;
  35. getline(cin,wish);
  36. if(wish=="erase"||wish=="Erase"){
  37. str = findErase(str, find);
  38.  
  39. cout << "result: " << str << endl;
  40. }
  41. else if(wish=="replace"||wish=="Replace"){
  42. cout<<"enter the replace: ";
  43. getline(cin,replace);
  44. str = findReplace(str, find, replace);
  45.  
  46. cout << "result: " << str << endl;
  47. }
  48. else{
  49. cout<<"invalid input"<<endl;
  50. cout<<"the valid words are:(erase , Erase , replace , Replace)";
  51. }
  52. return 0;
  53. }
Success #stdin #stdout 0s 5308KB
stdin
Standard input is empty
stdout
enter the string: enter the word you want to find: do you wish to erase it or to replace it?(erase,replace)invalid input
the valid words are:(erase , Erase , replace , Replace)