fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <set>
  4.  
  5. std::string escape(const char* src, const std::set<char> escapee, const char marker)
  6. {
  7. std::string r;
  8. while (char c = *src++)
  9. {
  10. if (escapee.find(c) != escapee.end())
  11. r += marker;
  12. // r += c;
  13. r += c == '\n' ? 'n' : c;
  14. }
  15. return r;
  16. }
  17.  
  18. int main()
  19. {
  20. std::string r = escape("\\this\" is a test\nthis is the second line", { ' ' }, '\\');
  21. std::cout << r;
  22. }
  23.  
  24.  
Success #stdin #stdout 0.01s 5284KB
stdin
Standard input is empty
stdout
\this"\ is\ a\ testnthis\ is\ the\ second\ line