Advertisement
Guest User

The Program That Writes Itself

a guest
Jul 6th, 2010
447
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <cstdio>
  2. #include <string>
  3.  
  4. const char* escape(std::string v)
  5. {
  6.     std::string r = "";
  7.     for (int i = 0; i < v.size(); i++)
  8.         if (v[i] == '\n')
  9.             r += "\\n";
  10.         else if (v[i] == '\t')
  11.             r += "\\t";
  12.         else
  13.             r += v[i];
  14.     return r.c_str();
  15. }
  16.  
  17. int main()
  18. {
  19.     std::string s = "#include <cstdio>\n#include <string>\n\nconst char* escape(std::string v)\n{\n\tstd::string r = %c%c;\n\tfor (int i = 0; i < v.size(); i++)\n\t\tif (v[i] == '%cn')\n\t\t\tr += %c%c%cn%c;\n\t\telse if (v[i] == '%ct')\n\t\t\tr += %c%c%ct%c;\n\t\telse\n\t\t\tr += v[i];\n\treturn r.c_str();\n}\n\nint main()\n{\n\tstd::string s = %c%s%c;\n\n\tprintf(s.c_str(),34,34,92,34,92,92,34,92,34,92,92,34,34,escape(s),34);\n\treturn 0;\n}";
  20.  
  21.     printf(s.c_str(),34,34,92,34,92,92,34,92,34,92,92,34,34,escape(s),34);
  22.     return 0;
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement