fork(1) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. static std::string ReplaceAll(std::string result, const std::string& src, const std::string& dest)
  5. {
  6. size_t pos = 0;
  7.  
  8. if (src == dest)
  9. return result;
  10.  
  11. while ((pos = result.find(src, pos)) != std::string::npos)
  12. {
  13. result.replace(pos, src.size(), dest);
  14. pos += dest.length();
  15. }
  16.  
  17. return result;
  18. }
  19.  
  20. int main()
  21. {
  22. std::cout << ReplaceAll("tetestst", "test", "") << std::endl;
  23. std::cout << ReplaceAll(" something with many spaces ", " ", " ") << std::endl;
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0s 3472KB
stdin
Standard input is empty
stdout
test
 something  with  many spaces