3
Contributors
7
Replies
8
Views
10 Years
Discussion Span
Last Post by DangerDev
0

Hi
Solve this:
write a program to generate its own source code.
(plz dnt come up with solution like, reading source file, etc.)

0

Also, generally, making quines is easy. You just have the following formula:

You'll make a program in three parts:
Part 1: If needed, make a function that prints a string once, and then prints it in the string literal notation you used to write the string, and then prints Part 3. Then in the 'main' function, pass the string you write in part 2 to this function.
Part 2: Write Part 1 in string form.
Part 3: This is the text that follows immediately after the string in Part 2.

0

Example:

#include <stdio.h>
#include <string.h>

void f(char* s) {
    char* p;
    p = s;
    printf("%s\"",p);
    while (*p) {
        if (strchr("\\\n\"", *p)) {
            printf("\\%c", *p == '\n' ? 'n' : *p);
        }
        else {
            putchar(*p);
        }
        ++p;
    }
    printf("\");\n}\n");
}

int main(void) {
    f("#include <stdio.h>\n#include <string.h>\n\nvoid f(char* s) {\n    char* p;\n    p = s;\n    printf(\"%s\\\"\",p);\n    while (*p) {\n        if (strchr(\"\\\\\\n\\\"\", *p)) {\n            printf(\"\\\\%c\", *p == '\\n' ? 'n' : *p);\n        }\n        else {\n            putchar(*p);\n        }\n        ++p;\n    }\n    printf(\"\\\");\\n}\\n\");\n}\n\nint main(void) {\n    f(");
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.