Replace all occurrences of character X with character Y in given string
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: xbxcd
Input: 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.
Code-
C++
#include <bits/stdc++.h>
using namespace std;
void replaceCharacter( char * str, char c1, char c2)
{
int j, n = strlen (str);
for ( int i = j = 0; i < n; i++) {
if (str[i] != c1) {
str[j++] = str[i];
}
else {
str[j++] = c2;
}
}
str[j] = '\0' ;
}
int main()
{
char str[] = "abacd" ;
char c1 = 'a' ;
char c2 = 'x' ;
replaceCharacter(str, c1, c2);
cout << str;
return 0;
}
|
Java
public class Main {
static void replaceCharacter( char [] str, char c1, char c2) {
int j = 0 ;
int n = str.length;
for ( int i = 0 ; i < n; i++) {
if (str[i] != c1) {
str[j++] = str[i];
} else {
str[j++] = c2;
}
}
}
public static void main(String[] args) {
char [] str = "abacd" .toCharArray();
char c1 = 'a' ;
char c2 = 'x' ;
replaceCharacter(str, c1, c2);
System.out.println(str);
}
}
|
Python3
def replaceCharacter( str , c1, c2):
n = len ( str )
res = ""
for i in range (n):
if str [i] ! = c1:
res + = str [i]
else :
res + = c2
return res
if __name__ = = '__main__' :
str = "abacd"
c1 = 'a'
c2 = 'x'
str = replaceCharacter( str , c1, c2)
print ( str )
|
C#
using System;
public class MainClass
{
static void ReplaceCharacter( char [] str, char c1, char c2)
{
int j = 0;
int n = str.Length;
for ( int i = 0; i < n; i++)
{
if (str[i] != c1)
{
str[j++] = str[i];
}
else
{
str[j++] = c2;
}
}
}
public static void Main( string [] args)
{
char [] str = "abacd" .ToCharArray();
char c1 = 'a' ;
char c2 = 'x' ;
ReplaceCharacter(str, c1, c2);
Console.WriteLine(str);
}
}
|
Javascript
function replaceCharacter(str, c1, c2) {
let result = "" ;
for (let i = 0; i < str.length; i++) {
if (str[i] != c1) {
result += str[i];
} else {
result += c2;
}
}
return result;
}
let str = "abacd" ;
let c1 = 'a' ;
let c2 = 'x' ;
let newStr = replaceCharacter(str, c1, c2);
console.log(newStr);
|
Output-
xbxcd
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.
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)
Below is the recursive implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void replaceCharacter( char input[],
char c1, char c2)
{
if (input[0] == '\0' ) {
return ;
}
if (input[0] == c1) {
input[0] = c2;
}
return replaceCharacter(input + 1,
c1, c2);
}
int main()
{
char str[] = "abacd" ;
char c1 = 'a' ;
char c2 = 'x' ;
replaceCharacter(str, c1, c2);
cout << str;
return 0;
}
|
Java
import java.util.*;
import java.io.*;
class GFG{
static String replaceCharacter(String str,
char c1, char c2)
{
if (str.length() == 1 )
{
return str;
}
char x=str.charAt( 0 );
if (str.charAt( 0 ) == c1)
{
x=c2;
str = c2+str.substring( 1 );
}
return x+replaceCharacter(str.substring( 1 ),
c1, c2);
}
public static void main(String[] args)
{
String str = "abacd" ;
char c1 = 'a' ;
char c2 = 'x' ;
System.out.println(replaceCharacter(str, c1, c2));
}
}
|
Python3
def replaceCharacter( input , c1, c2):
input = list ( str )
for i in range ( 0 , len ( str )):
if ( input [i] = = c1):
input [i] = c2;
print ( input [i], end = "")
str = "abacd"
c1 = 'a'
c2 = 'x'
replaceCharacter( str , c1, c2);
|
C#
using System;
class GFG{
static void replaceCharacter( string str,
char c1, char c2)
{
char [] input = str.ToCharArray();
for ( int i = 0; i < str.Length; i++)
{
if (input[i] == c1)
{
input[i] = c2;
}
Console.Write(input[i]);
}
}
public static void Main()
{
string str = "abacd" ;
char c1 = 'a' ;
char c2 = 'x' ;
replaceCharacter(str, c1, c2);
}
}
|
Javascript
<script>
function replaceCharacter(str,c1, c2)
{
if (str.length == 1)
{
return str;
}
var x=str.charAt(0);
if (str.charAt(0) == c1)
{
x=c2;
str = c2+str.substring(1);
}
return x+replaceCharacter(str.substring(1),
c1, c2);
}
var str = "abacd" ;
var c1 = 'a' ;
var c2 = 'x' ;
document.write(replaceCharacter(str, c1, c2));
</script>
|
Time Complexity: O(N), as we are using a loop to traverse N times so it will cost us O(N) time, where N is the length of the string.
Auxiliary Space: O(N), due to recursive stack space.
Last Updated :
05 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...