Add index to characters and reverse the string Last Updated : 07 Aug, 2022 Comments Improve Suggest changes Like Article Like Report Given a string str, the task is to encrypt and reverse the string. The string is encrypted by adding every character of the string with it's index in the string i.e. if character'a' is at index 2 then the character in the updated string will be 'a' + 2 = 'c'. Since value of string may go beyond 256, do the addition under modulo 256.Examples: Input: str = "geeks" Output: wngfg 'g' + 0 = 'g' 'e' + 1 = 'f' 'e' + 2 = 'g' 'k' + 3 = 'n' 's' + 4 = 'w' Input: str = "java" Output: dxbj Approach: Add every character of the string with its index within the same string.Reverse the string.Print the reversed string. Below is the implementation of the above approach: C++ // C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function to return the encryptet string string getEncryptedString(string str) { // To build the encrypted string string sb =""; // Traverse the string in reverse for (int i = str.length() - 1; i >= 0; i--) { sb+=(char)((int)str[i] + i) % 256; } // Return the encrypted string return sb; } // Driver code int main() { string str = "geeks"; cout<<getEncryptedString(str)<<endl; return 0; } // This code is contributed by mits Java // Java implementation of the approach public class HelloWorld { // Function to return the encryptet string static String getEncryptedString(String str) { // To build the encrypted string StringBuilder sb = new StringBuilder(""); // Traverse the string in reverse for (int i = str.length() - 1; i >= 0; i--) { sb.append("" + (char)((str.charAt(i) + i) % 256)); } // Return the encrypted string return sb.toString(); } // Driver code public static void main(String[] args) { String str = "geeks"; System.out.println(getEncryptedString(str)); } } Python3 # Python3 implementation of the approach # Function to return the encryptet string def getEncryptedString(str): # To build the encrypted string sb = ""; # Traverse the string in reverse for i in range(len(str) - 1,-1,-1): sb += chr((ord(str[i]) + i) % 256); # Return the encrypted string return sb; # Driver code str = "geeks"; print(getEncryptedString(str)); # This code is contributed by mits C# // C# implementation of the approach using System; using System.Text; public class HelloWorld { // Function to return the encryptet string static String getEncryptedString(String str) { // To build the encrypted string StringBuilder sb = new StringBuilder(""); // Traverse the string in reverse for (int i = str.Length - 1; i >= 0; i--) { sb.Append("" + (char)((str[i] + i) % 256)); } // Return the encrypted string return sb.ToString(); } // Driver code public static void Main(String[] args) { String str = "geeks"; Console.WriteLine(getEncryptedString(str)); } } // This code contributed by Rajput-Ji PHP <?php // PHP implementation of the approach // Function to return the encryptet string function getEncryptedString($str) { // To build the encrypted string $sb = ""; // Traverse the string in reverse for ($i = strlen($str) - 1; $i >= 0; $i--) { $sb .= chr((ord($str[$i]) + $i) % 256); } // Return the encrypted string return $sb; } // Driver code $str = "geeks"; print(getEncryptedString($str)); // This code is contributed by mits ?> JavaScript <script> // javascript implementation of the approach // Function to return the encryptet string function getEncryptedString( str) { // To build the encrypted string sb = ""; // Traverse the string in reverse for (i = str.length - 1; i >= 0; i--) { sb+=("" + String.fromCharCode((str.charCodeAt(i) + i) % 256)); } // Return the encrypted string return sb; } // Driver code var str = "geeks"; document.write(getEncryptedString(str)); // This code is contributed by Rajput-Ji </script> Output: wngfg Time complexity: O(N) where N is length of given string Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Add index to characters and reverse the string A Abhishek_Ranjan Follow Improve Article Tags : DSA Technical Scripter 2018 Reverse Practice Tags : Reverse Similar Reads Reverse a string without affecting special characters Given a string, that contains a special character together with alphabets ('a' to 'z' and 'A' to 'Z'), reverse the string in a way that special characters are not affected. Examples: Input: str = "a,b$c"Output: str = "c,b$a"Explanation: Note that $ and , are not moved anywhere. Only subsequence "abc 10 min read Reverse the given string in the range [L, R] Given a string str, and two integers L and R, the task is to reverse the string in the range [L, R] i.e. str[L...R].Examples: Input: str = "geeksforgeeks", L = 5, R = 7 Output: geeksrofgeeks Reverse the characters in the range str[5...7] = "geeksforgeeks" and the new string will be "geeksrofgeeks" I 4 min read Print words of a string in reverse order Let there be a string say "I AM A GEEK". So, the output should be "GEEK A AM I" . This can done in many ways. One of the solutions is given in Reverse words in a string . Examples: Input : I AM A GEEK Output : GEEK A AM I Input : GfG IS THE BEST Output : BEST THE IS GfG This can be done in more simp 10 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 Reverse String according to the number of words Given a string containing a number of words. If the count of words in string is even then reverse its even position's words else reverse its odd position, push reversed words at the starting of a new string and append the remaining words as it is in order. Examples: Input: Ashish Yadav Abhishek Rajp 6 min read Easiest Way to Reverse a String Have you wondered which is the easiest way to reverse a string, Imagine you are giving a contest and an algorithm you have made requires reversing a string, you must be aware of the easiest way to reverse a string. What is a reversal of string:Reversing a string is the technique that reverses or cha 4 min read Reverse the string whenever digit is encountered Given a string s of length N, the task is to traverse the string and reverse the substring from the start to the next encountered digit. After reversing the substring, update the start index to the character immediately following the digit. Example: Input: s = abc123def456ghiOutput: cba123fed456ihg 4 min read How to reverse a String in Python Reversing a string is a common task in Python, which can be done by several methods. In this article, we discuss different approaches to reversing a string. One of the simplest and most efficient ways is by using slicing. Letâs see how it works:Using string slicingThis slicing method is one of the s 4 min read Mirror characters of a string Given a string s and an integer k. The task is to perform a mirror transformation on the substring starting from the k-th position to the end of the string. In this transformation, each letter is replaced by its corresponding mirrored letter in the English alphabet ('a' -> 'z', 'b' -> 'y', 'c' 6 min read Reverse a String â Complete Tutorial Given a string s, the task is to reverse the string. Reversing a string means rearranging the characters such that the first character becomes the last, the second character becomes second last and so on.Examples:Input: s = "GeeksforGeeks"Output: "skeeGrofskeeG"Explanation : The first character G mo 13 min read Like