The Wayback Machine - https://web.archive.org/web/20240927184032/https://www.geeksforgeeks.org/code-to-generate-the-map-of-india-with-explanation/
Open In App

Code to Generate the Map of India (With Explanation)

Last Updated : 01 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Given an obfuscated code that generates the map of India, explain its working. The following code when executed generates the map of India.

An obfuscated code is a code that has been deliberately made difficult to understand or difficult to read, typically for the purpose of hiding its logic or making it harder for someone to reverse-engineer it. Obfuscated code is often used to protect proprietary algorithms or intellectual property, or to make it more difficult for attackers to understand and exploit security vulnerabilities in software.

However, obfuscation is not a foolproof way to secure software, as determined attackers can often reverse-engineer the code to understand its logic. Additionally, obfuscation can make code more difficult to maintain and debug, and can also slow down the performance of the code.

#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:

C++




// C++ program to print map of India
#include <iostream>
using namespace std;
 
int main()
{
    int a = 10, b = 0, c = 10;
 
    // The encoded string after removing first 31 characters
    // Its individual characters determine how many spaces
    // or exclamation marks to draw consecutively.
    char* str = "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!";
 
    while (a != 0)
    {
        // read each character of encoded string
        a = str[b++];
        while (a-- > 64)
        {
            if (++c == 90) // 'Z' is 90 in ascii
            {
                // reset c to 10 when the end of line is reached
                c = 10;        // '\n' is 10 in ascii
 
                // print newline
                putchar('\n'); // or putchar(c);
            }
            else
            {
                // draw the appropriate character
                // depending on whether b is even or odd
                if (b % 2 == 0)
                    putchar('!');
                else
                    putchar(' ');
            }
        }
    }
 
    return 0;
}
 
// This code is contributed by SHUBHAMSINGH10.


C




// C program to print map of India
#include <stdio.h>
 
int main()
{
    int a = 10, b = 0, c = 10;
 
    // The encoded string after removing first 31 characters
    // Its individual characters determine how many spaces
    // or exclamation marks to draw consecutively.
    char* str = "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!";
 
    while (a != 0)
    {
        // read each character of encoded string
        a = str[b++];
        while (a-- > 64)
        {
            if (++c == 90) // 'Z' is 90 in ascii
            {
                // reset c to 10 when the end of line is reached
                c = 10;        // '\n' is 10 in ascii
 
                // print newline
                putchar('\n'); // or putchar(c);
            }
            else
            {
                // draw the appropriate character
                // depending on whether b is even or odd
                if (b % 2 == 0)
                    putchar('!');
                else
                    putchar(' ');
            }
        }
    }
 
    return 0;
}


Java




// Java program to print map of India
class GFG
{
    public static void main(String[] args)
    {
        int a =10, b = 0, c = 10;
         
        // The encoded string after removing first 31 characters
        // Its individual characters determine how many spaces
        // or exclamation marks to draw consecutively.
        String s1="TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QP,\n"
        + "bEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelc,\n"
        + "lcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
         
        // read each character of encoded string
        a=s1.charAt(b);
         
        while (a != 0)
        {
            if (b < 170)
            {
                a = s1.charAt(b);
                b++;
                while (a-- > 64)
                {
                     
                    if (++c=='Z')
                    {
                         
                        c/=9;
                         
                        System.out.print((char)(c));
                    }
                    else
                        System.out.print((char)(33 ^ (b & 0x01)));
                }
            }
            else
                break;
        }
    }
}


Python3




# Python3 program to print map of India
a = 10
b = 0
c = 10
 
# The encoded string after removing first
# 31 characters. Its individual characters
# determine how many spaces or exclamation
# marks to draw consecutively.
s = ("TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs"
     " UJq TNn*RPn/QPbEWS_JSWQAIJO^NBELPe"
     "HBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFA"
     "gcEAelclcn^r^r\\tZvYxXyT|S~Pn SPm S"
     "On TNn ULo0ULo#ULo-WHq!WFs XDt!")
 
# Read each character of encoded string
a = ord(s[b])
 
while a != 0:
    if b < 170:
        a = ord(s[b])
        b += 1
         
        while a > 64:
            a -= 1
            c += 1
             
            if c == 90:
                c = c // 9
                print(end = chr(c))
            else:
                print(chr(33 ^ (b & 0X01)), end = '')
    else:
        break
 
# The code is contributed by aayush_chouhan


C#




// C# program to print map of India
using System;
 
class GFG
{
    public static void Main()
    {
        int a = 10, b = 0, c = 10;
         
        // The encoded string after removing first 31 characters
        // Its individual characters determine how many spaces
        // or exclamation marks to draw consecutively.
        string s1 = "TFy!QJu ROo TNn(ROo)SLq SLq ULo+UHs UJq TNn*RPn/QP,\n"
        + "bEWS_JSWQAIJO^NBELPeHBFHT}TnALVlBLOFAkHFOuFETpHCStHAUFAgcEAelc,\n"
        + "lcn^r^r\\tZvYxXyT|S~Pn SPm SOn TNn ULo0ULo#ULo-WHq!WFs XDt!";
         
        // read each character of encoded string
        a = s1[b];
         
        while (a != 0)
        {
            if (b < 170)
            {
                a = s1[b];
                b++;
                while (a-- > 64)
                {
                     
                    if (++c == 'Z')
                    {
                         
                        c/=9;
                         
                    Console.Write((char)(c));
                    }
                    else
                    Console.Write((char)(33 ^ (b & 0x01)));
                }
            }
            else
                break;
        }
    }
}
 
//This code is contributed by vt_m.


PHP




<?php
// PHP Implementation to
// print map of India
 
$a = 10;
$b = 0;
$c = 10;
 
// The encoded string after removing first 31 characters
// Its individual characters determine how many spaces
// or exclamation marks to draw consecutively.
$s1 = "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!";
$a=ord($s1[$b]);
 
while ($a != 0)
    {
        if ($b < 170)
        {
            $a = ord($s1[$b]);
            $b++;
            while ($a-- > 64)
            {
                 
                if (++$c==90)
                {
                     
                    $c=floor($c/9);
                     
                    echo chr($c);
                }
                else
                    printf(chr(33 ^ ($b & 0x01)));
            }
        }
        else
            break;
    }
 
// note: ord() function convert the
// characters into its Ascii value
 
//this code is contributed by mits
?>


Output: 

Console printing India's Map
 

The algorithm implemented by this code is as follows:

The algorithm implemented by this code is as follows:

1.Initialize three variables a, b, and c with values 10, 0, and 10 respectively.
2.Declare a character array str with a string of characters encoded by removing the first 31 characters of the original string.
3.Start a while loop that continues until a is 0.
a. Read the next character in str by incrementing the value of b and assigning the character at the new index of str to a.
b. Start another while loop that continues until a is less than or equal to 64.
i. If c is equal to 90, reset c to 10 and print a newline character.
ii. If c is not equal to 90, check if b is even or odd.
4.If b is even, print an exclamation mark.
5.If b is odd, print a space.
6.Increment c.
7.Decrement a.
8.Return 0.
This algorithm decodes the encoded string in str and draws a map of India using spaces and exclamation marks.



Previous Article
Next Article

Similar Reads

map::at() and map::swap() in C++ STL
Maps are the container in STL which is used to store the elements in the form of key-value pair. Internally, the elements in a map are always sorted by its key. Maps are mainly implemented as binary search trees. map::at() at() function is used to return the reference to the element associated with the key k. Syntax: map1.at(k) Parameters: k is the
3 min read
Searching in a map using std::map functions in C++
Usually, main purpose of using map stl container is for efficient search operations and sorted order retrieval. As map stores key-value pair, all the search operations take "O(log(n))" time (n is size of map). Different types of search functions exists in C++ language, each having different functions. In the context of competitive programming, this
7 min read
map vs unordered_map in C++
Pre-requisite : std::map, std::unordered_mapWhen it comes to efficiency, there is a huge difference between maps and unordered maps. We must know the internal working of both to decide which one is to be used. Difference : | map | unordered_map --------------------------------------------------------- Ordering | increasing order | no ordering | of
2 min read
map::clear() in C++ STL
Map is dictionary like data structure. It is an associative array of (key, value) pair, where only single value is associated with each unique key. map::clear() clear() function is used to remove all the elements from the map container and thus leaving it's size 0. Syntax: map1.clear() where map1 is the name of the map. Parameters: No parameters ar
2 min read
map::size() in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::size() In C++, size() function is used to return the total number of elements present in the map. Syntax: map_name.size() Return Value: It returns the number of elements presen
1 min read
map::begin() and end() in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::begin() begin() function is used to return an iterator pointing to the first element of the map container. begin() function returns a bidirectional iterator to the first elemen
3 min read
map::empty() in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::empty() empty() function is used to check if the map container is empty or not. Syntax : mapname.empty() Parameters : No parameters are passed. Returns : True, if map is empty
1 min read
map::operator[] in C++ STL
Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. map::operator[] This operator is used to reference the element present at position given inside the operator. It is similar to the at() function, the only difference is that the at(
2 min read
Default values in a Map in C++ STL
Prerequisite: Map in STLA map is a container which is used to store a key-value pair. By default, In Primitive datatypes such as int, char, bool, float in C/C++ are undefined if variables are not initialized, But a Map is initially empty when it is declared. When this map is accessed with the [ ] (e.g map<int,int> mpp; mpp[1]; ) if the key is
2 min read
C++ map having key as a user define data type
C++ map stores keys in ordered form (Note that it internally use a self balancing binary search tree). Ordering is internally done using operator " < " So if we use our own data type as key, we must overload this operator for our data type. Let us consider a map having key data type as a structure and mapped value as integer. // key's struc
3 min read
Which data structure is used by Map?
What is a Map? Before learning the data structure used by a map, let us have an overlook of map. Map is the part of the STL library that stores key value pairs in it and no two values have the same keys but the different keys can store similar values. The map stores keys in sorted order. These are some functions which map uses with their Time Compl
2 min read
set vs map in C++ STL
set and map in STL are similar in the sense that they both use Red Black Tree (A self balancing BST). Note that the time complexities of search, insert and delete are O(Log n). Differences: The difference is set is used to store only keys while map is used to store key value pairs. For example consider in the problem of printing sorted distinct ele
4 min read
map emplace() in C++ STL
The map::emplace() is a built-in function in C++ STL which inserts the key and its element in the map container. It effectively increases the container size by one. If the same key is emplaced more than once, the map stores the first element only as the map is a container which does not store multiple keys of the same value. Syntax: map_name.emplac
2 min read
map cbegin() and cend() function in C++ STL
map::cbegin() is a built-in function in C++ STL that returns a constant iterator referring to the first element in the map container. Since map container contains the element in an ordered way, cbegin() will point to that element that will come first according to the container's sorting criterion. Syntax: map_name.cbegin()Parameters: The function d
3 min read
map crbegin() and crend() function in C++ STL
map::crbegin() is a built-in function in C++ STL that returns a constant reverse iterator referring to the last element in the map container. Since map container contains the element in an ordered way, crbegin() will point to that element that will come last according to the container's sorting criterion. Syntax: map_name.crbegin()Parameters: The f
3 min read
map max_size() in C++ STL
The map::max_size() is a built-in function in C++ STL which returns the maximum number of elements a map container can hold. Syntax: map_name.max_size() Parameters: This function does not accept any parameters. Return Value: This function returns the maximum number of elements a map container can hold. C/C++ Code // C++ program to illustrate // map
1 min read
map upper_bound() function in C++ STL
The map::upper_bound() is a built-in function in C++ STL which returns an iterator pointing to the immediate next element just greater than k. If the key passed in the parameter exceeds the maximum key in the container, then the iterator returned points to the number of elements in the map container as key and element=0. Syntax: map_name.upper_boun
2 min read
map operator= in C++ STL
The map::operator= is a built function in C++ STL which assigns contents of a container to a different container, replacing its current content. Syntax: map1_name = map2_name Parameters: The map on the left is the container in which the map on the right is to be assigned by destroying the elements of map1. Return Value: This function does not retur
2 min read
map emplace_hint() function in C++ STL
The map::emplace_hint() is a built-in function in C++ STL which inserts the key and its element in the map container with a given hint. It effectively increases the container size by one as map is the container that stores keys with the element value. The hint provided does not affect the position to be entered, it only increases the speed of inser
2 min read
map equal_range() in C++ STL
The map::equal_range() is a built-in function in C++ STL which returns a pair of iterators. The pair refers to the bounds of a range that includes all the elements in the container which have a key equivalent to k. Since the map container only contains unique key, hence the first iterator in the pair returned thus points to the element and the seco
2 min read
map count() function in C++ STL
The map::count() is a built-in function in C++ STL which returns 1 if the element with key K is present in the map container. It returns 0 if the element with key K is not present in the container. Syntax: map_name.count(key k) Parameters: The function accepts a mandatory parameter k which specifies the key to be searched in the map container. Retu
2 min read
map rend() function in C++ STL
The rend() function is an inbuilt function in C++ STL which returns a reverse iterator pointing to the theoretical element right before the first key-value pair in the map(which is considered its reverse end). Syntax: map_name.rend() Parameters:The function does not take any parameter. Return Value: The function returns a reverse iterator pointing
2 min read
map rbegin() function in C++ STL
The std::map::rbegin() is a function in C++ STL. It returns a reverse iterator which points to the last element of the map. The reverse iterator iterates in reverse order and incrementing it means moving towards beginning of map. Syntax: r_i rbegin(); const_r_i rbegin() const; Parameters: It does not except any parameters. Returns: It returns a rev
2 min read
map value_comp() in C++ STL
The std::map::value_comp() is a function in C++ STL. It returns a function object that compares objects of type std::map::value. Syntax: value_compare value_comp() const Parameters: It does not accept any parameters. Returns: This method returns a function object that compares objects of type std::map::value. Time Complexity: O(1) Below examples il
2 min read
map key_comp() function in C++ STL
The map::key_comp() is a function in STL in C++ that returns a copy of comparison object used by container that compare keys. Syntax: map.key_comp() Return value: This method returns the comparison object used by container that compare keys. Below examples illustrate the working of key_comp() method: Example: C/C++ Code // C++ program to demonstrat
2 min read
How to traverse a STL map in reverse direction?
Map stores the elements in sorted order of keys. Now if we want to traverse it in reverse order we will use reverse_iterator of map. Syntax: map::reverse_iterator iterator_name; Reverse Iterator of map moves in backward direction on increment. So, we will point the reverse_iterator to the last element of map and then keep on incrementing it until i
8 min read
How to delete last element from a map in C++
If we wish to delete the last element from a map, we can use following methods : using prev(mp.end()) : what prev function do is, it goes back one step back from the given iterator. So using prev function with mp.end() will return an iterator which points the last element of the map. Implementation: #include <bits/stdc++.h> using namespace st
2 min read
map get_allocator in C++ STL
map::get_allocator() is a built in function in C++ STL which is used to get allocator of container map. Syntax: Allocator_type get_allocator() Parameters: This function does not accept any parameter. Return value: Returns an allocator associated with map. Below programs explains clearly the map::get_allocator() function. Example-1: // CPP program t
1 min read
Minimum and Maximum of all subarrays of size K using Map
Given an array arr[] of N integers and an integer K, the task is to find the minimum and maximum of all subarrays of size K. Examples: Input: arr[] = {2, -2, 3, -9, -5, -8}, K = 4 Output: -9 3 -9 3 -9 3 Explanation: Below are the subarray of size 4 and minimum and maximum value of each subarray: 1. {2, -2, 3, -9}, minValue = -9, maxValue = 3 2. {-2
9 min read
Map of Vectors in C++ STL with Examples
Map in STL Maps are associative containers that store elements in a mapped fashion. Each element has a key value and a mapped value. No two mapped values can have same key values. Vector in STL Vector is same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled au
2 min read