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