Skip to content

Instantly share code, notes, and snippets.

@tjb0607
Last active August 29, 2015 14:16
Show Gist options
  • Save tjb0607/69038a4ff74cb9b27051 to your computer and use it in GitHub Desktop.
Save tjb0607/69038a4ff74cb9b27051 to your computer and use it in GitHub Desktop.
#include <iostream>
using namespace std;
char* EncodedPrint(char* string)
{
int i = 0;
while (string[i] != '\0')
{
switch (string[i])
{
case '\\':
cout << "\\\\";
break;
case '\n':
cout << "\\n";
break;
case '\t':
cout << "\\t";
break;
case '\"':
cout << "\\\"";
break;
default:
cout << string[i];
break;
}
i++;
}
return string + i + 1;
}
int main()
{
char quine[] = "#include <iostream>\nusing namespace std;\n\nchar* EncodedPrint(char* string)\n{\n\tint i = 0;\n\twhile (string[i] != '\\0')\n\t{\n\t\tswitch (string[i])\n\t\t{\n\t\tcase '\\\\':\n\t\t\tcout << \"\\\\\\\\\";\n\t\t\tbreak;\n\t\tcase '\\n':\n\t\t\tcout << \"\\\\n\";\n\t\t\tbreak;\n\t\tcase '\\t':\n\t\t\tcout << \"\\\\t\";\n\t\t\tbreak;\n\t\tcase '\\\"':\n\t\t\tcout << \"\\\\\\\"\";\n\t\t\tbreak;\n\t\tdefault:\n\t\t\tcout << string[i];\n\t\t\tbreak;\n\t\t}\n\t\ti++;\n\t}\n\treturn string + i + 1;\n}\n\nint main()\n{\n\tchar quine[] = \"\0\";\n\n\tcout << quine;\n\tchar* rest = EncodedPrint(quine);\n\tcout << \"\\\\0\";\n\tEncodedPrint(rest);\n\tcout << rest;\n}\n";
cout << quine;
char* rest = EncodedPrint(quine);
cout << "\\0";
EncodedPrint(rest);
cout << rest;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment