Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

The Program That Writes Itself

By: a guest on Jul 6th, 2010  |  syntax: C++  |  size: 0.86 KB  |  views: 341  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  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. }