Replace every character of a string by a different character
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.
Below is the implementation of above approach:
C++
// C++ program for Replace every character of a // string by a different character #include <bits/stdc++.h> using namespace std; // Function to manipulate the string void 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')); // coverting '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 code int main() { // Declaring str as 'geeksforgeeks' string str = "geeksforgeeks"; manipulateString(str); cout << str; return 0; } |
Java
// Java program for Replace every character of a // string by a different character 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)); // coverting '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
# Python 3 program for Replace every character of a # string by a different character # Function to manipulate the string def 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'))) # coverting '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 # charcters 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#
// C# program for Replace every character of a // string by a different character using 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)); // coverting '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 |
fbbnddvbfbbnd
Recommended Posts:
- Replace every character of string by character whose ASCII value is K times more than it
- Replace a character c1 with c2 and c2 with c1 in a string S
- Modify the string such that every character gets replaced with the next character in the keyboard
- Find a string such that every character is lexicographically greater than its immediate next character
- Map every character of one string to another such that all occurrences are mapped to the same character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Longest Common Prefix using Character by Character Matching
- Count substrings that starts with character X and ends with character Y
- Shortest distance to every other character from given character
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Find the character in first string that is present at minimum index in second string
- Convert the string into palindrome string by changing only one character.
- Queries to find the last non-repeating character in the sub-string of a given string
- Queries to find the first non-repeating character in the sub-string of a string
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.


