# How to decrypt a string according to given algorithm?

Decryption or decrypt a string is the process used to protect confidential and sensitive data from hackers. It converts encrypted data or text into its original form. Encryption is the process of converting plain text into an unreadable and non-understandable cipher text format so that hackers cannot understand it. These processes are interrelated and involve various algorithms for processing. We decrypt the string according to the following algorithm.

• If the string length is odd. For odd index values append alphabets from the back of the string and for even index values append alphabets from the front of the input encrypted string.

• If the string length is even: For odd index values append alphabets from the front of the string and for even index values append alphabets from the back of the input encrypted string.

### Demonstration 1

Input = Encrypted string = "olhel"
Output = hello


### Explanation

Decrypt the input string by first finding the length of the string. Add the alphabet in reverse order The string length is 5 (odd) so, for the odd index value we add alphabets from the back, and for the even index value, we append alphabets from the front in the following manner. After every alphabet addition, move to the next alphabet.

• The alphabet at index value 1 is "o". Add 'o' to the resulting string. The result is 'o'.

• The alphabet at index value 2 is "l". Add 'l' to the resulting string. The result is 'lo'.

• The alphabet at index value 3 is "h". Add 'l' to the resulting string. The result is 'llo'.

• The alphabet at index value 4 is "e". Add 'e' to the resulting string. The result is 'ello'.

• The alphabet at index value 5 is "l". Add 'h' to the resulting string. The result is 'hello'.

### Demonstration 2

Input = Encrypted string = "flie"
Output = life


### Explanation

Decrypt the input string using the algorithm, first finding the length of the string. Add alphabets in the reverse order. The string length is 4 (even) so, for odd index values we add alphabets from the front, and for even index values, append alphabets from the back in the following manner. After every alphabet addition, move to the next alphabet.

• The alphabet at index value 1 is "f". Add 'e' to the resulting string. The result is 'e'.

• The alphabet at index value 2 is "l". Add 'f' to the resulting string. The result is 'fe'.

• The alphabet at index value 3 is "i". Add 'i' to the resulting string. The result is 'ife'.

• The alphabet at index value 4 is "e". Add 'l' to the resulting string. The result is 'life'.

### C++ Library Function

• length() − it is a string class library function, defined in the <string> header file. It returns the length of the input string in byte form. It counts total string characters.

string_name.length();

• size() − It is a predefined function in C++ library which returns the input string length.

string_name.size();

• reverse() − It is defined in the C++ standard library. It reverses the order of the string by considering the starting and ending position of the string. It takes two parameters: starting index value and the ending index value.

reverse(string_name.begin(), string_name.end());


### Algorithm

• Take an encrypted string that is encrypted using the above-mentioned encryption rules.

• Insert alphabets to the resulting string in reverse order.

• Traverse the string using the 'for' loop.

• Find the length of the string using the length() function.

• If the length of the input string is even: for odd place values, add characters from the front, and for even place values add characters from the back of the input encrypted string.

• If the length of the input string is odd: for odd place va;ues. Add characters from the back of the input string and for even place values add characters front the front of the encrypted string.

• Print the resulting decrypted string.

### Example

Implement the problem statement in this tutorial by taking a string as input. Decrypt the input string by considering the rules listed above. Reverse the final string to get the desired Output.

#include <bits/stdc++.h>
using namespace std;
string decryptedString(string str, int len) {
string result = "";
if (len % 2)  {
int cntVar = 0; int start = 0, last = len - 1;
while (result.size() != len) {
if (cntVar % 2 == 0)
result += str[start++];
else
result += str[last--];
cntVar++;
}
} else {
int cntVar = 0; int start = 0, last = len - 1;
while (result.size() != len) {
if (cntVar % 2 == 0)
result += str[last--];
else
result += str[start++];
cntVar++;
}
}
reverse(result.begin(), result.end());
return result;
}
int main() {
string str = "flie";
int len = str.length();
cout << "The decrypt string according to the following algorithm is : " << decryptedString(str, len);
return 0;
}


### Output

The decrypt string according to the following algorithm is : life


## Conclusion

As we reach the end of this tutorial, we use an algorithm to decrypt the string. Define the rules or algorithm to decrypt the string. We implemented the approach using C++ and the reverse() function to reverse the resulting string. Some demonstrations are explained in the tutorial to elaborate on the task's meaning and solution.

Updated on: 03-Oct-2023

28 Views 