Encode given string by shifting each character forward with its alphabetical value Last Updated : 27 Jun, 2023 Comments Improve Suggest changes Like Article Like Report Given string str of size N consisting of lowercase English alphabets, the task is to encode the given string as follows: change every character of that string to another characterthe distance between the changed character and the current character is the same as the distance between the current character and 'a'.Also, assume that the character's array forms a cycle, i.e. after 'z' the cycle starts again from 'a'. Examples: Input: str = "geeksforgeeks"Output: "miiukkcimiiuk"Explanation:g changed to m (distance between g & a is 6, distance between m & g is 6)e changed to i (distance between e & a is 4, distance between i & e is 4)and same for other characters as well. Input: str = "cycleofalphabet"Output: "ewewickaweoacim" Approach: This problem can be solved using the following steps: Run a loop from i=0 to i<N and traverse each character of the string. For each character str[i]:Find the distance between str[i] and 'a', i.e. dist=str[i]-'a'.Now, if (dist+(str[i]-'a')) > 26, this means that 'z' is exceeded, soOtherwise, change str[i] to str[i]+dist.Print the string as the answer to this problem. Below is the implementation of the above approach: C++ // C++ code for the above approach #include <bits/stdc++.h> using namespace std; // Function to change every character // of the string to another character void changeString(string str) { for (auto& x : str) { int dist = x - 'a'; // If 'z' is exceeded if (dist + (x - 'a') >= 26) { dist = (dist + (x - 'a')) % 26; x = 'a' + dist; } // If 'z' is not exceeded else { x = x + dist; } } cout << str << endl; } // Driver Code int main() { string str = "nayan"; changeString(str); return 0; } Java // Jsvs code for the above approach import java.util.*; class GFG { // Function to change every character // of the string to another character static void changeString(String str) { char[] ch = str.toCharArray(); for (int i = 0; i < str.length(); i++) { int dist = ch[i] - 'a'; // If 'z' is exceeded if (dist + (ch[i] - 'a') >= 26) { dist = (dist + (ch[i] - 'a')) % 26; ch[i] = (char)('a' + dist); } // If 'z' is not exceeded else { ch[i] = (char)(ch[i] + dist); } } String s = new String(ch); System.out.println(s); } // Driver Code public static void main(String[] args) { String str = "cycleofalphabet"; changeString(str); } } // This code is contributed by ukasp. Python3 # Python code for the above approach # Function to change every character # of the string to another character def changeString(str): str = list(str) for x in range(len(str)): dist = ord(str[x]) - ord('a') # If 'z' is exceeded if (dist + (ord(str[x]) - ord('a')) >= 26): dist = (dist + (ord(str[x]) - ord('a'))) % 26; str[x] = chr(ord('a') + dist); # If 'z' is not exceeded else: str[x] = chr(ord(str[x]) + dist); str = "".join(str) print(str) # Driver Code str = "cycleofalphabet"; changeString(str); # This code is contributed by Saurabh Jaiswal C# // C# code for the above approach using System; using System.Collections; class GFG { // Function to change every character // of the string to another character static void changeString(string str) { char[] ch = str.ToCharArray(); for(int i = 0; i < str.Length; i++) { int dist = ch[i] - 'a'; // If 'z' is exceeded if (dist + (ch[i] - 'a') >= 26) { dist = (dist + (ch[i] - 'a')) % 26; ch[i] = (char)('a' + dist); } // If 'z' is not exceeded else { ch[i] = (char)(ch[i] + dist); } } string s = new string(ch); Console.WriteLine(s); } // Driver Code public static void Main() { string str = "cycleofalphabet"; changeString(str); } } // This code is contributed by Samim Hossain Mondal. JavaScript <script> // JavaScript code for the above approach // Function to change every character // of the string to another character function changeString(str) { str = str.split('') for (let x = 0; x < str.length; x++) { let dist = str[x].charCodeAt(0) - 'a'.charCodeAt(0); // If 'z' is exceeded if (dist + (str[x].charCodeAt(0) - 'a'.charCodeAt(0)) >= 26) { dist = (dist + (str[x].charCodeAt(0) - 'a'.charCodeAt(0))) % 26; str[x] = String.fromCharCode('a'.charCodeAt(0) + dist); } // If 'z' is not exceeded else { str[x] = String.fromCharCode(str[x].charCodeAt(0) + dist); } } str = str.join('') document.write(str + "<br>") } // Driver Code let str = "cycleofalphabet"; changeString(str); // This code is contributed by Potta Lokesh </script> Outputewewickaweoacim Time Complexity: O(N)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Encode given string by shifting each character forward with its alphabetical value harshdeepmahajan88 Follow Improve Article Tags : Strings DSA encoding-decoding strings Practice Tags : StringsStrings Similar Reads Check if the characters of a given string are in alphabetical order Given a string 's', the task is to find if the characters of the string are in alphabetical order. The string contains only lowercase characters. Examples: Input: Str = "aabbbcc" Output: In alphabetical order Input: Str = "aabbbcca" Output: Not in alphabetical order A simple approach: Store the stri 14 min read Minimize cost to empty a given string by removing characters alphabetically Given string str, the task is to minimize the total cost to remove all the characters from the string in alphabetical order. The cost of removing any character at i th index from the string will be i. The indexing is 1-based. Examples: Input: str = "abcab" Output: 8 Explanation: First char âaâ at in 7 min read Modify a string by circularly shifting each character to the right by respective frequencies Given a string S consisting of lowercase English alphabets, the task is to right shift each character of the given string S circularly by its frequency. Circular shifting of characters refers to shifting character 'z' to 'a', as its next character. Examples: Input: S = "geeksforgeeks"Output: iiimugp 6 min read Find Array formed by reversing Prefix each time given character is found Given an array arr[] of length N consisting of uppercase English letters only and a letter ch. the task is to find the final array that will form by reversing the prefix each time the letter ch is found in the array. Examples: Input: arr[] = {'A', 'B', 'X', 'C', 'D', 'X', 'F'}, ch= 'X'Output: D C X 6 min read Find the character made by adding all the characters of the given string Given a string str consisting of lowercase English alphabets. The task is to add all the character values i.e. 'a' = 1, 'b' = 2, 'c' = 3, ..., 'z' = 26 and output the character corresponding to the sum value. If it exceeds 26 then take sum % 26. Examples: Input: str = "gfg" Output: t (g + f + g) = 7 7 min read Modify string by increasing each character by its distance from the end of the word Given a string S, the task is to modify the given string by replacing every character S[i] by a new character whose value is (S[i] + its position from the end of the word) Examples: Input: S = "acm fkz" Output: "cdm hlz" Explanation: There are 2 words in the given string {"acm", "fkz"} For "acm": a 7 min read Modify string by replacing characters by alphabets whose distance from that character is equal to its frequency Given a string S consisting of N lowercase alphabets, the task is to modify the string S by replacing each character with the alphabet whose circular distance from the character is equal to the frequency of the character in S. Examples: Input: S = "geeks"Output: hggltExplanation: The following modif 7 min read Check if string S can be converted to T by incrementing characters Given strings S and T. The task is to check if S can be converted to T by performing at most K operations. For the ith operation, select any character in S which has not been selected before, and increment the chosen character i times (i.e., replacing it with the letter i times ahead in the alphabet 8 min read Count characters at same position as in English alphabet Given a string of lower and uppercase characters, the task is to find how many characters are in the same position as in the English alphabet. Examples: Input: ABcED Output : 3 First three characters are at same position as in English alphabets. Input: geeksforgeeks Output : 1 Only 'f' is at same po 7 min read How to insert characters in a string at a certain position? Given a string str and an array of indices chars[] that describes the indices in the original string where the characters will be added. For this post, let the character to be inserted in star (*). Each star should be inserted before the character at the given index. Return the modified string after 7 min read Like