Program to find the Encrypted word - GeeksforGeeks

02 Mar, 2020
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++
Java
Python3
C#
// C++ program to implement // the above approach  #include <bits/stdc++.h>using namespace std;  // Function to find the encrypted stringvoid 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 codeint main(){    string s = "abcd";    findWord(s, s.length());    return 0;}
// Java program to implement the above approachimport java.util.*;import java.io.*;  class GFG{  // Static function declared to find// the encrypted stringpublic 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 codepublic static void main(String args[]){    String s = "abcd";    findWord(s, s.length());}}  // This code is contributed by Animesh_Gupta
# Python3 program to implement # the above approach  # Function to find the encrypted stringdef 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 codes = "abcd"findWord(s, len(s))  # This code is contributed by SHUBHAMSINGH10
// C# program to implement the above approachusing System;  class GFG{  // Static function declared to find// the encrypted stringpublic 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 codepublic static void Main(String []args){    String s = "abcd";    findWord(s, s.Length);}}  // This code is contributed by PrinciRaj1992
bdee

Time Complexity: O(N)
Animesh_Gupta
princiraj1992
SHUBHAMSINGH10
cryptography
School Programming
Strings
Strings
cryptography
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Constructors in Java
Exceptions in Java
Ternary Operator in Python
Difference between Abstract Class and Interface in Java
Destructors in C++
Write a program to reverse an array or string
Longest Common Subsequence | DP-4
Write a program to print all permutations of a given string
Check for Balanced Brackets in an expression (well-formedness) using Stack
KMP Algorithm for Pattern Searching