fork download
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4. int main()
  5. {
  6. cout << "Enter a string:\n";
  7. string array_1;
  8. getline(cin, array_1);
  9.  
  10. cout << "Enter a string you want to find:\n";
  11. string ch;
  12. getline(cin, ch);
  13.  
  14. cout << "Enter a string you wish to replace " << ch << " with:\n";
  15. string ch2;
  16. cin >> ch2;
  17.  
  18. for(size_t pos = array_1.find(ch); pos != std::string::npos;
  19. pos = array_1.find(ch, pos))
  20. {
  21. array_1.replace(pos, ch.size(), ch2);
  22. pos += ch2.size();
  23. }
  24.  
  25. cout << "Here is the new string: " << array_1 << '\n';
  26. }
  27.  
Success #stdin #stdout 0.02s 2860KB
stdin
abcllo abcre I am
abc
he
stdout
Enter a string:
Enter a string you want to find:
Enter a string you wish to replace abc with:
Here is the new string: hello here I am