Given a string str and two characters X and Y, the task is to write a recursive function to replace all occurrences of character X with character Y.
Examples:
Input: str = abacd, X = a, Y = x
Output: xbxcdInput: str = abacd, X = e, Y = y
Output: abacd
Iterative Approach: The idea is to iterate over the given string and if any character X is found then replace that character with Y.
Time Complexity: O(N)
Auxiliary Space: O(1)
Recursive Approach: The idea is to recursively traverse the given string and replace the character with value X with Y. Below are the steps:
- Get the string str, character X, and Y.
- Recursively Iterate from index 0 to string length.
- Base Case: If we reach the end of the string the exit from the function.
if(str[0]=='\0') return ;
- Recursive Call: If the base case is not met, then check for the character at 0th index if it is X then replace that character with Y and recursively iterate for next character.
if(str[0]==X) str[0] = Y
- Return Statement: At each recursive call(except the base case), return the recursive function for the next iteration.
return recursive_function(str + 1, X, Y)
- Base Case: If we reach the end of the string the exit from the function.
Below is the recursive implementation:
C++
// C++ program for the above approach #include <bits/stdc++.h> using namespace std; // Function to replace all occurrences // of character c1 with character c2 void replaceCharacter(char input[], char c1, char c2) { // Base Case // If the string is empty if (input[0] == '\0') { return; } // If the character at starting // of the given string is equal // to c1, replace it with c2 if (input[0] == c1) { input[0] = c2; } // Getting the answer from recursion // for the smaller problem return replaceCharacter(input + 1, c1, c2); } // Driver Code int main() { // Given string char str[] = "abacd"; char c1 = 'a'; char c2 = 'x'; // Function call replaceCharacter(str, c1, c2); // Print the string cout << str; return 0; } |
Java
// Java program for the above approach import java.util.*; import java.io.*; class GFG{ // Function to replace all occurrences // of character c1 with character c2 static void replaceCharacter(String str, char c1, char c2) { char[] input = str.toCharArray(); // If the character at starting // of the given string is equal // to c1, replace it with c2 for(int i = 0; i < str.length(); i++) { if (input[i] == c1) { input[i] = c2; } // Print the string System.out.print(input[i]); } } // Driver Code public static void main(String[] args) { // Given string String str = "abacd"; char c1 = 'a'; char c2 = 'x'; // Function call replaceCharacter(str, c1, c2); } } // This code is contributed by sanjoy_62 |
Python3
# Python3 program for the above approach # Function to replace all occurrences # of character c1 with character c2 def replaceCharacter(input, c1, c2): input = list(str) # If the character at starting # of the given string is equal # to c1, replace it with c2 for i in range(0, len(str)): if (input[i] == c1): input[i] = c2; # Print the string print(input[i], end = "") # Driver Code # Given string str = "abacd"c1 = 'a'c2 = 'x' # Function call replaceCharacter(str, c1, c2); # This code is contributed by sanjoy_62 |
C#
// C# program for the above approach using System; class GFG{ // Function to replace all occurrences // of character c1 with character c2 static void replaceCharacter(string str, char c1, char c2) { char[] input = str.ToCharArray(); // If the character at starting // of the given string is equal // to c1, replace it with c2 for(int i = 0; i < str.Length; i++) { if (input[i] == c1) { input[i] = c2; } // Print the string Console.Write(input[i]); } } // Driver Code public static void Main() { // Given string string str = "abacd"; char c1 = 'a'; char c2 = 'x'; // Function call replaceCharacter(str, c1, c2); } } // This code is contributed by sanjoy_62 |
xbxcd
Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(1)
Recommended Posts:
- Replace all occurrences of pi with 3.14 in a given string
- Recursive program to replace all occurrences of pi with 3.14 in a given string
- Replace all occurrences of string AB with C without using extra space
- Replace all occurrences of a string with space
- Map every character of one string to another such that all occurrences are mapped to the same character
- Number of permutations of a string in which all the occurrences of a given character occurs together
- 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
- Remove all occurrences of a character in a string
- Remove all occurrences of a character in a string | Recursive approach
- In-place replace multiple occurrences of a pattern
- Check if all occurrences of a character appear together
- Minimize ASCII values sum after removing all occurrences of one character
- Print the string by ignoring alternate occurrences of any character
- Count occurrences of a character in a repeated string
- Sum of all distances between occurrences of same characters in a given string
- Count occurrences of a string that can be constructed from another given string
- Replace a character c1 with c2 and c2 with c1 in a string S
- Minimize cost to replace all the vowels of a given String by a single vowel
- Swap all occurrences of two characters to get lexicographically smallest 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.
Improved By : sanjoy_62


