Friday 29 March 2013

C/C++ Puzzles: PART - 31

Printing the Map of India

  Yes, Yet another obfuscated C Program. But this one is interesting. The program is printing the map of India. I got this program from stackoverflow. But the name of the author was not specified. But he deserves full credit for this brilliant piece of code. The program is shown below.

#include <Stdio.h>
main()
{
    int a,b,c;
    int count = 1;
    printf("\n\n");
    for (b=c=10;a="- FIGURE?, UMKC,XYZHello Folks,\
    TFy!QJu ROo TNn(ROo)SLq SLq ULo+\
    UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^\
    NBELPeHBFHT}TnALVlBLOFAkHFOuFETp\
    HCStHAUFAgcEAelclcn^r^r\\tZvYxXy\
    T|S~Pn SPm SOn TNn ULo0ULo#ULo-W\
    Hq!WFs XDt!" [b+++21]; )
    for(; a-- > 64 ; )
    putchar ( ++c=='Z' ? c = c/ 9:33^b&1);
    printf("\n\n");
    return 0;
}

Click here to download the C Program.


Figure Below Shows the Output


The program is obfuscated to hide the working from casual onlookers. But the version given below might help you understand the working of the program.


#include <stdio.h>

int main (void) 
{
    int a=10, b=0, c=10;
    char* bits ="TFy!QJu ROo TNn(ROo)SLq 
    SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}
    TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn 
    SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
    a = bits[b];
    printf("\n\n");
    while (a != 0) 
    {
        a = bits[b];
        b++;
        while (a > 64)
        {
            a--;
            if (++c == 'Z') 
            {
                c /= 9;
                putchar(c);
            } 
            else 
            {
                putchar(33 ^ (b & 0x01));
            }
        }
    }
    printf("\n\n");
    return 0;
}


Click here to download the C Program.

The string contains the run length encoding of the map to be printed. The run length encoding tells us how many times to print each character. The statement putchar(c) prints the newline character and the statement putchar(33 ^ (b & 0x01)) prints the character !.

No comments:

Post a Comment