Given a string, the given string is an encrypted word, the task is to decrypt the given string to get the original word.
Examples:
Input: str = "abcd" Output: bdee Explanation: a -> a + 1 -> b b -> b + 2 -> d c -> c + 2 -> e d -> d + 1 -> e Input: str = "xyz" Output: yaa Explanation: x -> x + 1 -> y y -> y + 2 -> a z -> z + 1 -> a
Approach:
- Let the length of the string be n.
- then the encrypted string will be:
- Print the string after finding the scrypted word.
Below is the implementation of the above approach:
C++
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std; // Function to find the encrypted string void findWord(string c, int n) { int co = 0, i; // to store the encrypted string string s(n, ' '); for (i = 0; i < n; i++) { if (i < n / 2) co++; else co = n - i; // after 'z', it should go to a. if (c[i] + co <= 122) s[i] = (char)((int)c[i] + co); else s[i] = (char)((int)c[i] + co - 26); } cout << s; } // Driver code int main() { string s = "abcd"; findWord(s, s.length()); return 0; } |
chevron_right
filter_none
Java
// Java program to implement the above approach import java.util.*; import java.io.*; class GFG { // Static function declared to find // the encrypted string public static void findWord(String c, int n) { int co = 0, i; // Character array to store //the encrypted string char s[] = new char[n]; for (i = 0; i < n ; i++) { if (i < n / 2) co++; else co = n - i; // after 'z', it should go to a. if ((c.charAt(i) + co) <= 122) s[i] = (char)((int)c.charAt(i) + co); else s[i] = (char)((int)c.charAt(i) + co - 26); } // storing the character array in the string. String str = Arrays.toString(s); System.out.println(str); } // Driver code public static void main(String args[]) { String s = "abcd"; findWord(s, s.length()); } } // This code is contributed by Animesh_Gupta |
chevron_right
filter_none
Python3
# Python3 program to implement # the above approach # Function to find the encrypted string def findWord(c, n): co = 0 # to store the encrypted string s = [0] * n for i in range(n): if (i < n / 2): co += 1 else: co = n - i # after 'z', it should go to a. if (ord(c[i]) + co <= 122): s[i] = chr(ord(c[i]) + co) else: s[i] = chr(ord(c[i]) + co - 26) print(*s, sep = "") # Driver code s = "abcd"findWord(s, len(s)) # This code is contributed by SHUBHAMSINGH10 |
chevron_right
filter_none
C#
// C# program to implement the above approach using System; class GFG { // Static function declared to find // the encrypted string public static void findWord(String c, int n) { int co = 0, i; // Character array to store // the encrypted string char []s = new char[n]; for (i = 0; i < n ; i++) { if (i < n / 2) co++; else co = n - i; // after 'z', it should go to a. if ((c[i] + co) <= 122) s[i] = (char)((int)c[i] + co); else s[i] = (char)((int)c[i] + co - 26); } // storing the character array in the string. String str = String.Join("",s); Console.WriteLine(str); } // Driver code public static void Main(String []args) { String s = "abcd"; findWord(s, s.Length); } } // This code is contributed by PrinciRaj1992 |
chevron_right
filter_none
Output:
bdee
Time Complexity: O(N)
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.
Recommended Posts:
- Decrypt a string encrypted by repeating i-th character i times
- Restore original String from given Encrypted String by the given operations
- C program to find and replace a word in a File by another given word
- C program to Replace a word in a text by another given word
- Find the word from a given sentence having given word as prefix
- Longest Common Prefix using Word by Word Matching
- Program to find Smallest and Largest Word in a String
- Java program to count the characters in each word in a given sentence
- Program for length of the longest word in a sentence
- Program to replace a word with asterisks in a sentence
- Python program for word guessing game
- Find largest word in dictionary by deleting some characters of given string
- Find shortest unique prefix for every word in a given list | Set 2 (Using Sorting)
- Find the first repeated word in a string
- Find frequency of each word in a string in Python
- Find the first maximum length even word from a string
- Find the count of sub-strings whose characters can be rearranged to form the given word
- Find the word with most anagrams in a given sentence
- Print list items containing all characters of a given word
- Recursively print all sentences that can be formed from list of word lists
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.

