fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4.  
  5. int main() {
  6. std::string const toReplace = "ing";
  7. std::string const replacement = "ed";
  8. std::string const separators = " ";
  9.  
  10. std::string string = " Hello hing and ning and ting ying ";
  11.  
  12. std::string::size_type first = 0, last = 0;
  13. while (true) {
  14. first = string.find_first_not_of(separators, last);
  15. if (first == std::string::npos) {
  16. break;
  17. }
  18.  
  19. last = string.find_first_of(separators, first);
  20. if (last == std::string::npos) {
  21. break;
  22. }
  23.  
  24. std::string::size_type wordLength = last - first;
  25. if (wordLength >= toReplace.length()) {
  26. std::string::size_type tailFirst = last - toReplace.length();
  27. if (string.compare(tailFirst, toReplace.length(), toReplace) == 0) {
  28. string.replace(tailFirst, toReplace.length(), replacement);
  29. }
  30. }
  31. }
  32.  
  33. std::cout << string << std::endl;
  34. }
  35.  
  36.  
Success #stdin #stdout 0s 3428KB
stdin
Standard input is empty
stdout
 Hello  hed and   ned and ted yed