# Decrypt a string according to given rules

Given an encrypted string str, the task is to decrypt the given string when the encryption rules are as follows:

2. In every odd step, append the next character to it.
3. In every even step, prepend the next character to the encrypted string so far.

For example, if str = “geeks” then the encrypted string will be,
g -> ge -> ege -> egek -> segek

Examples:

Input: str = “segosegekfrek”
Output: geeksforgeeks

Input: str = “vrstie”
Output: strive

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The steps that are given to encrypt the string can be followed in reverse order to obtain the original string. There will be two conditions to obtain the original string:

• If the string is of odd length, in odd step add the characters from the back in the resultant string, else add from the front.
• If the string is of even length, in odd step add characters from the front and add characters from back in even step.

The reverse of the resultant string thus obtained is the original string which was encrypted.

Below is the implementation of the above approach:

 `// C++ program to decrypt the original string ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return the original string  ` `// after decryption ` `string decrypt(string s, ``int` `l) ` `{ ` `    ``// Stores the decrypted string ` `    ``string ans = ``""``; ` ` `  `    ``// If length is odd ` `    ``if` `(l % 2) { ` ` `  `        ``// Step counter ` `        ``int` `cnt = 0; ` ` `  `        ``// Starting and ending index ` `        ``int` `indl = 0, indr = l - 1; ` ` `  `        ``// Iterate till all characters  ` `        ``// are decrypted ` `        ``while` `(ans.size() != l) { ` ` `  `            ``// Even step ` `            ``if` `(cnt % 2 == 0) ` `                ``ans += s[indl++]; ` ` `  `            ``// Odd step ` `            ``else` `                ``ans += s[indr--]; ` ` `  `            ``cnt++; ` `        ``} ` `    ``} ` ` `  `    ``// If length is even ` `    ``else` `{ ` ` `  `        ``// Step counter ` `        ``int` `cnt = 0; ` ` `  `        ``// Starting and ending index ` `        ``int` `indl = 0, indr = l - 1; ` `        ``while` `(ans.size() != l) { ` ` `  `            ``// Even step ` `            ``if` `(cnt % 2 == 0) ` `                ``ans += s[indr--]; ` ` `  `            ``// Odd step ` `            ``else` `                ``ans += s[indl++]; ` ` `  `            ``cnt++; ` `        ``} ` `    ``} ` ` `  `    ``// Reverse the decrypted string ` `    ``reverse(ans.begin(), ans.end()); ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``string s = ``"segosegekfrek"``; ` `    ``int` `l = s.length(); ` `    ``cout << decrypt(s, l); ` ` `  `    ``return` `0; ` `} `

Output:

```geeksforgeeks
```

My Personal Notes arrow_drop_up

Striver(underscore)79 at Codechef and codeforces D

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.