code in c to generate map of india

Code to generate the map of India using the c language.

In this article, I am writing a c code to generate the map of INDIA. This program is an example of obfuscated code in C language. Here we will take an encoded long string. The long string is simply a binary sequence of characters.


Here string is a run-length encoding of the map of India. In which alternating characters stores how many times to draw space, and how many times to draw an exclamation mark consecutively.

// Encoded string
char *pzEncodedString = "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!";

 

 

Note:  Here I am using Backslash at every end of the line to make the string continuous. If we miss the writing backslash at the end of every line then we will get the compiler error.

 

For learning more, you can signup for the free trial of this popular c video course by Kenny Kerr.

In the below program, the outer loop is used to get the characters from the encoded string. In each iteration, fetches the characters and increases the index.

The inner loop is used to display the individual characters like an exclamation mark (!) or space (‘ ’). This loop is also asserted new line whenever it reaches the end of the line.

The inner loop is drawn (CharacterAsciValue – 64 ) character and AsciValueNewLine is going from 10 to 90. The value of AsciValueNewLine is reset to 10 when the end of the line is reached.

#include <stdio.h>

int main (void)
{
    int cAsciValue = 0;
    int cIndex = 0;
    int newLineAsci = 10;

// Encoded string
    char *pzEncodedString = "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!";


    cAsciValue = pzEncodedString[cIndex];

    while (cAsciValue != 0)
    {
        //Fetch character from index and increment index.
        cAsciValue = pzEncodedString[cIndex++];

        // Ascii value '9' is 64
        while (cAsciValue > 64)
        {
            cAsciValue--;
            // Asci Value of 'Z' is 90
            if (++newLineAsci == 90)
            {
                newLineAsci = 10;   // Reset c 10 when

                putchar(newLineAsci); // Assert New Line
            }
            else
            {
                //If cIndex is even then print '!'
                //either print ' ' (space)
                putchar(33 ^ (cIndex & 0x01));
            }
        }
    }
    return 0;
}

OutPut:

 

Recommended posts: