Replace every character of a string by a different character

27 May, 2022
Given a string str of lowercase alphabets only. The task is to replace every character by some new character. The new character belongs to (small case only) Replacement of str[i] is determined by: 
str[i] = Character obtained after traversing ascii(str[i]) no. of characters in ‘a-z’ repeatedly after str[i].
Examples:  
Input: str = “geeksforgeeks” Output: fbbnddvbfbbndIn the above case str = “geeksforgeeks”, the ASCII value of ‘g’ is 103 therefore ‘g’ has been replaced by moving 103 times from ‘g’ in the desired range i.e., a-z. Hence, character ‘g’ is replaced by ‘f’. Similarly, the complete string “geeksforgeeks” becomes “fbbnddvbfbbnd”.Input: str = “science” Output: dxjbtxb
 
Approach:  
Traverse the string.For every i, find the character that needs to be replaced with str[i].Replace str[i] with that character.
Traverse the string.
For every i, find the character that needs to be replaced with str[i].
Replace str[i] with that character.
Below is the implementation of above approach:  
C++
Java
Python 3
C#
Javascript
// C++ program for Replace every character of a// string by a different character#include <bits/stdc++.h>using namespace std; // Function to manipulate the stringvoid manipulateString(string &str){     // looping through each character of string    for (int i = 0; i < str.length(); i++) {         // storing integer ASCII value of        // the character in 'asc'        int asc = str[i];         // 'rem' contains coded value which        // needs to be rounded to 26        int rem = asc - (26 - (str[i] - 'a'));         // converting 'rem' character in range        // 0-25 and storing in 'm'        int m = rem % 26;         // printing character by adding ascii value of 'a'        // so that it becomes in the desired range i.e. a-z        str[i] = (char)(m + 'a');    }} // Driver codeint main(){     // Declaring str as 'geeksforgeeks'    string str = "geeksforgeeks";     manipulateString(str);     cout << str;     return 0;}
// Java program for Replace every character of a// string by a different characterpublic class GFG {     //Function to manipulate the string    static void manipulateString(String str)    {         char[] str1 = str.toCharArray();              // looping through each character of string     for (int i = 0; i < str.length(); i++) {          // storing integer ASCII value of         // the character in 'asc'                   int asc = str1[i];                 // 'rem' contains coded value which         // needs to be rounded to 26         int rem = asc - (26 - (str1[i] - 97));          // converting 'rem' character in range         // 0-25 and storing in 'm'         int m = rem % 26;                  // printing character by adding ascii value of 'a'         // so that it becomes in the desired range i.e. a-z         str1[i] = (char)(m + 'a');                  }          String str2 = String.valueOf(str1);     System.out.println(str2);          }     //Driver code    public static void main(String[] args) {                 // Declaring str as 'geeksforgeeks'         String str = "geeksforgeeks";          manipulateString(str);                       }}
# Python 3 program for Replace every character of a# string by a different character # Function to manipulate the stringdef manipulateString(str) :     # looping through each character of string    for i in range(len(str)) :                 # storing integer ASCII value of        # the character in 'asc'        asc = ord(str[i])         # 'rem' contains coded value which        # needs to be rounded to 26        rem = asc - (26 - (ord(str[i]) - ord('a')))         # converting 'rem' character in range        # 0-25 and storing in 'm'        m  = rem % 26         #printing character by adding ascii value of 'a'        # so that it becomes in the desired range i        str[i] = chr(m + ord('a'))     # join method join all individual    # characters to form a string    print(''.join(str)) # Driver code    if __name__ == "__main__" :     str = "geeksforgeeks"     # convert string into list of characters    str = list(str)     # Function calling    manipulateString(str)   # This code is contributed by ANKITRAI1
     // C# program for Replace every character of a// string by a different characterusing System;public class GFG {      //Function to manipulate the string    static void manipulateString(String str)    {          char[] str1 = str.ToCharArray();               // looping through each character of string     for (int i = 0; i < str.Length; i++) {           // storing integer ASCII value of         // the character in 'asc'                    int asc = str1[i];                  // 'rem' contains coded value which         // needs to be rounded to 26         int rem = asc - (26 - (str1[i] - 97));           // converting 'rem' character in range         // 0-25 and storing in 'm'         int m = rem % 26;                   // printing character by adding ascii value of 'a'         // so that it becomes in the desired range i.e. a-z         str1[i] = (char)(m + 'a');                   }           String str2 = String.Join("",str1);     Console.WriteLine(str2);           }      //Driver code    public static void Main() {                  // Declaring str as 'geeksforgeeks'         String str = "geeksforgeeks";         manipulateString(str);                         }}// This code is contributed by PrinciRaj1992
<script> // Javascript program for Replace every character of a// string by a different character  // Function to manipulate the stringfunction manipulateString(str){     // looping through each character of string    for (var i = 0; i < str.length; i++) {         // storing integer ASCII value of        // the character in 'asc'        var asc = str[i];         // 'rem' contains coded value which        // needs to be rounded to 26        var rem = asc.charCodeAt(0) - (26 - (str[i].charCodeAt(0) - 'a'.charCodeAt(0)));         // converting 'rem' character in range        // 0-25 and storing in 'm'        var m = (rem % 26);         // printing character by adding ascii value of 'a'        // so that it becomes in the desired range i.e. a-z        str[i] = String.fromCharCode(m + 'a'.charCodeAt(0));    }    return str;} // Driver code// Declaring str as 'geeksforgeeks'var str = "geeksforgeeks";document.write(manipulateString(str.split('')).join('')); </script>
fbbnddvbfbbnd
 
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time.Auxiliary Space: O(1), as we are not using any extra space.
ankthon
ukasp
princiraj1992
noob2000
simmytarika5
saurabh1990aror
rohitsingh07052
ASCII
Strings
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Python program to check if a string is palindrome or not
Longest Palindromic Substring | Set 1
Length of the longest substring without repeating characters
Convert string to char array in C++
Top 50 String Coding Problems for Interviews
Check whether two strings are anagram of each other
What is Data Structure: Types, Classifications and Applications
Reverse words in a given string
Print all the duplicates in the input string
Reverse string in Python (6 different ways)