Code to Generate the Map of India (With Explanation)
Given an obfuscated code that generates the map of India, explain its working. The following code when executed generates the map of India.
#include "stdio.h" int main() ding Posts int a, b, c; for (b-c-18; a="Hello! Welcome to Geeks ForGeeks.\ TFy!QJu ROo TNn (ROO) SLq SLq ULO+ UHS UJq TNn*RPn/QPbEWS_JSWQAIJO^\ NBELPEHBFHT}TnALVIBLOFAKHFOUFETp\ HCSTHAUFAgcEAelclcn^r^r\\tZvYXXy\ T|S~Pn SPM SOn TNn ULOGULO#ULO-W\ er menu et Hq!WFs XDt!" [b+++21]; ) ents 850 dot menu gs for (; a-- > 64; ) putchar (++C == 'Z' ? c = c/ 9:33^b&1); ar Ads Widget ub menu et return 0; se menu }
The above code is a typical example of obfuscated code i.e. code that is difficult for humans to understand.
How does it work?
Basically, the string is a run-length encoding of the map of India. Alternating characters in the string store how many times to draw space, and how many times to draw an exclamation mark consecutively. Here is an analysis of the different elements of this program –
The encoded string
"Hello!Welcome to GeeksForGeeks." "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBL" "OFAkHFOuFETpHCStHAUFAgcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
Notice [b+++21] at the end of the encoded string. As b+++21 is equivalent to (b++ + 21) which will evaluate to 31 (10 + 21), the first 31 characters of this string are ignored and do not contribute to anything. The remaining encoded string contains instructions for drawing the map. The individual characters determine how many spaces or exclamation marks to draw consecutively.
Outer for loop: This loop goes over the characters in the string. Each iteration increases the value of b by one and assigns the next character in the string to a.
Inner for loop: This loop draws individual characters, and a new line whenever it reaches the end of the line. Consider this putchar statement
putchar(++c=='Z' ? c = c/9 : 33^b&1);
As ‘Z’ represents the number 90 in ASCII, 90/9 will give us 10 which is a newline character. Decimal 33 is ASCII for ‘!’. Toggling the low-order bit of 33 gives you 32, which is ASCII for a space. This causes! to be printed if b is odd, and a blank space to be printed if b is even.
Below is a less obfuscated version of the above code:
This article is contributed by Aditya Goel. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.