Replace a character c1 with c2 and c2 with c1 in a string S
Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1.
Examples:
Input : grrksfoegrrks,
c1 = e, c2 = r
Output : geeksforgeeks
Input : ratul,
c1 = t, c2 = h
Output : rahul
Traverse through the string and check for the occurrences of c1 and c2. If c1 is found then replace it with c2 and else if c2 is found replace it with c1.
C++
// CPP program to replace c1 with c2 // and c2 with c1 #include <bits/stdc++.h> using namespace std; string replace(string s, char c1, char c2) { int l = s.length(); // loop to traverse in the string for (int i = 0; i < l; i++) { // check for c1 and replace if (s[i] == c1) s[i] = c2; // check for c2 and replace else if (s[i] == c2) s[i] = c1; } return s; } // Driver code to check the above function int main() { string s = "grrksfoegrrks"; char c1 = 'e', c2 = 'r'; cout << replace(s, c1, c2); return 0; } |
chevron_right
filter_none
Java
// Java program to replace c1 with c2 // and c2 with c1 class GFG { static String replace(String s, char c1, char c2) { int l = s.length(); char []arr = s.toCharArray(); // loop to traverse in the string for (int i = 0; i < l; i++) { // check for c1 and replace if (arr[i] == c1) arr[i] = c2; // check for c2 and replace else if (arr[i] == c2) arr[i] = c1; } return String.valueOf(arr); } // Driver code public static void main(String []args) { String s = "grrksfoegrrks"; char c1 = 'e', c2 = 'r'; System.out.println(replace(s, c1, c2)); } } // This code is contributed // by ChitraNayal |
chevron_right
filter_none
Python3
# Python3 program to replace c1 with c2 # and c2 with c1 def replace(s, c1, c2): l = len(s) # loop to traverse in the string for i in range(l): # check for c1 and replace if (s[i] == c1): s = s[0:i] + c2 + s[i + 1:] # check for c2 and replace elif (s[i] == c2): s = s[0:i] + c1 + s[i + 1:] return s # Driver Code if __name__ == '__main__': s = "grrksfoegrrks" c1 = 'e' c2 = 'r' print(replace(s, c1, c2)) # This code is contributed # by PrinciRaj1992 |
chevron_right
filter_none
C#
// C# program to replace c1 with c2 // and c2 with c1 using System; public class GFG{ static String replace(String s, char c1, char c2) { int l = s.Length; char []arr = s.ToCharArray(); // loop to traverse in the string for (int i = 0; i < l; i++) { // check for c1 and replace if (arr[i] == c1) arr[i] = c2; // check for c2 and replace else if (arr[i] == c2) arr[i] = c1; } return string.Join("", arr); } // Driver code public static void Main() { String s = "grrksfoegrrks"; char c1 = 'e', c2 = 'r'; Console.WriteLine(replace(s, c1, c2)); } } // This code is contributed by 29AjayKumar |
chevron_right
filter_none
PHP
<?php // PHP program to replace c1 // with c2 and c2 with c1 function replace($s, $c1, $c2) { $l = strlen($s); // loop to traverse // in the string for ($i = 0; $i < $l; $i++) { // check for c1 and replace if ($s[$i] == $c1) $s[$i] = $c2; // check for c2 and replace else if ($s[$i] == $c2) $s[$i] = $c1; } return $s; } // Driver Code $s = "grrksfoegrrks"; $c1 = 'e'; $c2 = 'r'; echo replace($s, $c1, $c2); // This code is contributed by anuj_67. ?> |
chevron_right
filter_none
Output:
geeksforgeeks
Time Complexity: O(n)
Recommended Posts:
- Replace every character of string by character whose ASCII value is K times more than it
- Replace every character of a string by a different character
- Find a string such that every character is lexicographically greater than its immediate next character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Lexicographically smallest string formed by appending a character from first K characters of a string | Set 2
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Replace two substrings (of a string) with each other
- Replace all occurrences of pi with 3.14 in a given string
- Find the character in first string that is present at minimum index in second string
- URLify a given string (Replace spaces is %20)
- Replace all occurrences of a string with space
- Convert the string into palindrome string by changing only one character.
- Queries to find the first non-repeating character in the sub-string of a string
- Queries to find the last non-repeating character in the sub-string of a given 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.



