# Decrypt the String according to given algorithm

Given an encrypted string str consisting of alphabets and numeric characters, the task is to decrypt the string and find the encrypted message.
In order to decrypt the message, find every cluster of numeric characters representing a single alphabetic character which can be obtained by calculating the modulus of the number with 26 and the found value from the range [0, 25] can be mapped with the characters [‘a’, ‘z’].
For example, if str = “32ytAAcV4ui30hf10hj18” then the clusters of numeric characters will be {32, 4, 30, 10, 18} which gives {6, 4, 4, 10, 18} after performing modulus with 26 and can be mapped to {‘g’, ‘e’, ‘e’, ‘k’, ‘s’}

Examples:

Input: str = “this50hi4huji70”
Output: geeks

Input: str = “a1s2d3f3”
Output: bcdd

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

Approach: The idea is to traverse each character one by one in a string, then check if it is a numeric character or not. If it is then concatenate it into string. Finally, modulo that number string with 26.
While doing modulo operation, we can’t simply do x % 26 as number can be too large and will result into integer overflow.
To handle this, we will process all digits one by one and use the property that
(A * B) mod C = ((A mod C) * (B mod C)) mod C

Then, finally decode back each integer to character by 0, 1, 2, 3, …25 to ‘a’, ‘b’, ‘c’, …’z’, concatenate all characters and print the final result.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the approach ` `#include ` `using` `namespace` `std; ` `const` `int` `MOD = 26; ` ` `  `// Function that returns (num % 26) ` `int` `modulo_by_26(string num) ` `{ ` `    ``// Initialize result ` `    ``int` `res = 0; ` ` `  `    ``// One by one process all digits of 'num' ` `    ``for` `(``int` `i = 0; i < num.length(); i++) ` `        ``res = (res * 10 + (``int``)num[i] - ``'0'``) % MOD; ` ` `  `    ``return` `res; ` `} ` ` `  `// Function to return the decrypted string ` `string decrypt_message(string s) ` `{ ` ` `  `    ``// To store the final decrypted answer ` `    ``string decrypted_str = ``""``; ` ` `  `    ``string num_found_so_far = ``""``; ` ` `  `    ``// One by one check for each character ` `    ``// if it is a numeric character ` `    ``for` `(``int` `i = 0; i < s.length(); ++i) { ` ` `  `        ``if` `(s[i] >= ``'0'` `&& s[i] <= ``'9'``) { ` `            ``num_found_so_far += s[i]; ` `        ``} ` `        ``else` `if` `(num_found_so_far.length() > 0) { ` ` `  `            ``// Modulo the number found in the string by 26 ` `            ``decrypted_str += ``'a'` `                             ``+ modulo_by_26(num_found_so_far); ` ` `  `            ``num_found_so_far = ``""``; ` `        ``} ` `    ``} ` ` `  `    ``if` `(num_found_so_far.length() > 0) { ` `        ``decrypted_str += ``'a'` `                         ``+ modulo_by_26(num_found_so_far); ` `    ``} ` ` `  `    ``return` `decrypted_str; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` `    ``string s = ``"32ytAAcV4ui30hf10hj18"``; ` ` `  `    ``// Print the decrypted string ` `    ``cout << decrypt_message(s); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation of the approach ` `import` `java.util.*; ` ` `  `class` `GFG{ ` `     `  `static` `int` `MOD = ``26``; ` ` `  `// Function that returns (num % 26) ` `static` `int` `modulo_by_26(String num) ` `{ ` `     `  `    ``// Initialize result ` `    ``int` `res = ``0``; ` ` `  `    ``// One by one process all digits of 'num' ` `    ``for``(``int` `i = ``0``; i < num.length(); i++) ` `    ``{ ` `       ``res = ((res * ``10` `+  ` `              ``(``int``)num.charAt(i) - ``'0'``) % MOD); ` `    ``} ` `    ``return` `res; ` `} ` ` `  `// Function to return the decrypted String ` `static` `String decrypt_message(String s) ` `{ ` ` `  `    ``// To store the final decrypted answer ` `    ``String decrypted_str = ``""``; ` `    ``String num_found_so_far = ``""``; ` ` `  `    ``// One by one check for each character ` `    ``// if it is a numeric character ` `    ``for``(``int` `i = ``0``; i < s.length(); ++i) ` `    ``{ ` `       ``if` `(s.charAt(i) >= ``'0'` `&& s.charAt(i) <= ``'9'``) ` `       ``{ ` `           ``num_found_so_far += s.charAt(i); ` `       ``} ` `       ``else` `if` `(num_found_so_far.length() > ``0``) ` `       ``{ ` `            `  `           ``// Modulo the number found in the String by 26 ` `           ``decrypted_str += (``char``)(``'a'` `+  ` `                             ``modulo_by_26(num_found_so_far)); ` `           ``num_found_so_far = ``""``; ` `       ``} ` `    ``} ` `    ``if` `(num_found_so_far.length() > ``0``) ` `    ``{ ` `        ``decrypted_str += (``char``)(``'a'` `+  ` `                          ``modulo_by_26(num_found_so_far)); ` `    ``} ` `    ``return` `decrypted_str; ` `} ` ` `  `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` `    ``String s = ``"32ytAAcV4ui30hf10hj18"``; ` ` `  `    ``// Print the decrypted string ` `    ``System.out.print(decrypt_message(s)); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

## C#

 `// C# implementation of the approach ` `using` `System; ` `class` `GFG{ ` `     `  `static` `int` `MOD = 26; ` ` `  `// Function that returns (num % 26) ` `static` `int` `modulo_by_26(String num) ` `{ ` `     `  `    ``// Initialize result ` `    ``int` `res = 0; ` ` `  `    ``// One by one process all digits of 'num' ` `    ``for``(``int` `i = 0; i < num.Length; i++) ` `    ``{ ` `        ``res = ((res * 10 +  ` `               ``(``int``)num[i] - ``'0'``) % MOD); ` `    ``} ` `    ``return` `res; ` `} ` ` `  `// Function to return the decrypted String ` `static` `String decrypt_message(String s) ` `{ ` ` `  `    ``// To store the readonly decrypted answer ` `    ``String decrypted_str = ``""``; ` `    ``String num_found_so_far = ``""``; ` ` `  `    ``// One by one check for each character ` `    ``// if it is a numeric character ` `    ``for``(``int` `i = 0; i < s.Length; ++i) ` `    ``{ ` `        ``if` `(s[i] >= ``'0'` `&& s[i] <= ``'9'``) ` `        ``{ ` `            ``num_found_so_far += s[i]; ` `        ``} ` `        ``else` `if` `(num_found_so_far.Length > 0) ` `        ``{ ` `                 `  `            ``// Modulo the number found  ` `            ``// in the String by 26 ` `            ``decrypted_str += (``char``)(``'a'` `+  ` `                              ``modulo_by_26(num_found_so_far)); ` `            ``num_found_so_far = ``""``; ` `        ``} ` `    ``} ` `    ``if` `(num_found_so_far.Length > 0) ` `    ``{ ` `        ``decrypted_str += (``char``)(``'a'` `+  ` `                          ``modulo_by_26(num_found_so_far)); ` `    ``} ` `    ``return` `decrypted_str; ` `} ` ` `  `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` `    ``String s = ``"32ytAAcV4ui30hf10hj18"``; ` ` `  `    ``// Print the decrypted string ` `    ``Console.Write(decrypt_message(s)); ` `} ` `} ` ` `  `// This code is contributed by amal kumar choubey `

Output:

```geeks
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

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.

Improved By : Amal Kumar Choubey