fork(3) download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. int main()
  5. {
  6. std::string str = "t\tt\\";
  7.  
  8. std::cout << "Before: " << str << std::endl;
  9.  
  10. std::string::size_type pos = 0;
  11. while ((pos = str.find_first_of("\t\n\\", pos)) != std::string::npos)
  12. {
  13. switch (str[pos])
  14. {
  15. case '\t':
  16. str.replace(pos, 1, "\\t");
  17. pos += 2;
  18. break;
  19. case '\n':
  20. str.replace(pos, 1, "\\n");
  21. pos += 2;
  22. break;
  23. case '\\':
  24. str.replace(pos, 1, "\\\\");
  25. pos += 4;
  26. break;
  27. }
  28. }
  29.  
  30. std::cout << "After: " << str << std::endl;
  31.  
  32. return 0;
  33. }
Success #stdin #stdout 0s 5464KB
stdin
Standard input is empty
stdout
Before: t	t\
After: t\tt\\