Skip to content

Instantly share code, notes, and snippets.

@kadirmalak
Last active September 13, 2017 19:11
Show Gist options
  • Save kadirmalak/2df5394617848ec16c570675db3305e3 to your computer and use it in GitHub Desktop.
Save kadirmalak/2df5394617848ec16c570675db3305e3 to your computer and use it in GitHub Desktop.
Self Reproducing Program (Quine) in C++
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
auto source = vector<string>{
"#include <iostream>",
"#include <string>",
"#include <vector>",
"",
"using namespace std;",
"",
"int main()",
"{",
" auto source = vector<string>{",
" {SRC}",
" };",
"",
" for (auto line : source)",
" {",
" if (line == \" {SRC}\")",
" {",
" for (auto line2 : source)",
" {",
" cout << \" \" << '\"';",
" for (auto ch : line2)",
" {",
" if (ch == '\\\\' || ch == '\"') {",
" cout << '\\\\';",
" }",
" cout << ch;",
" }",
" cout << '\"' << \",\" << endl;",
" }",
" }",
" else {",
" cout << line << endl;",
" }",
" }",
"",
" return 0;",
"}",
"",
};
for (auto line : source)
{
if (line == " {SRC}")
{
for (auto line2 : source)
{
cout << " " << '"';
for (auto ch : line2)
{
if (ch == '\\' || ch == '"') {
cout << '\\';
}
cout << ch;
}
cout << '"' << "," << endl;
}
}
else {
cout << line << endl;
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment