Hi all
Post some challenging programming question, which are really hard to solve...

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

^^ A Perl solution

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.

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(");
}

its nice sarehu...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.