Remove all occurrences of a character in a string | Recursive approach
Last Updated :
31 May, 2022
Given string str, the task is to write a recursive program to remove all the occurrences of a character X in the string.
Examples:
Input: str = “geeksforgeeks”, c = ‘e’
Output: gksforgks
Input: str = “geeksforgeeks”, c = ‘g’
Output: eeksforeeks
Iterative Approach: The iterative approach to this problem can be found in this post.
Recursive Approach: Below are the steps:
- Get the string str and character X for which character X is to be removed.
- Recursively iterate for all the character in the string:
- Base Case: If the length of the string str called recursively is 0 then return the empty string from the function.
if(str.length()==0) {
return "";
}
- Recursive Call: If the base case is not met, then check for the character at 0th index if it is X then recursively iterate for the substring removing the first character.
if (str[0] == X) {
return recursive_function(str.substr(1), X);
}
- Return Statement: At each recursive call(except the base case and the above condition), return the recursive function for the next iteration including the character at 0th index.
return str[0] + recursive_function(str.substr(1), X)
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string removeCharRecursive(string str,
char X)
{
if (str.length() == 0) {
return "" ;
}
if (str[0] == X) {
return removeCharRecursive(str.substr(1), X);
}
return str[0]
+ removeCharRecursive(str.substr(1), X);
}
int main()
{
string str = "geeksforgeeks" ;
char X = 'e' ;
str = removeCharRecursive(str, X);
cout << str;
return 0;
}
|
Java
class GFG{
static String removeCharRecursive(String str,
char X)
{
if (str.length() == 0 )
{
return "" ;
}
if (str.charAt( 0 ) == X)
{
return removeCharRecursive(
str.substring( 1 ), X);
}
return str.charAt( 0 ) +
removeCharRecursive(
str.substring( 1 ), X);
}
public static void main(String[] args)
{
String str = "geeksforgeeks" ;
char X = 'e' ;
str = removeCharRecursive(str, X);
System.out.println(str);
}
}
|
Python3
def removeCharRecursive( str , X):
if ( len ( str ) = = 0 ):
return ""
if ( str [ 0 ] = = X):
return removeCharRecursive( str [ 1 :], X)
return str [ 0 ] + removeCharRecursive( str [ 1 :], X)
str = "geeksforgeeks"
X = 'e'
str = removeCharRecursive( str , X)
print ( str )
|
C#
using System;
class GFG{
static String removeCharRecursive(String str,
char X)
{
if (str.Length == 0)
{
return "" ;
}
if (str[0] == X)
{
return removeCharRecursive(
str.Substring(1), X);
}
return str[0] + removeCharRecursive(
str.Substring(1), X);
}
public static void Main(String[] args)
{
String str = "geeksforgeeks" ;
char X = 'e' ;
str = removeCharRecursive(str, X);
Console.WriteLine(str);
}
}
|
Javascript
<script>
function removeCharRecursive(str,X)
{
if (str.length == 0)
{
return "" ;
}
if (str.charAt(0) == X)
{
return removeCharRecursive(
str.substring(1), X);
}
return str.charAt(0) +
removeCharRecursive(
str.substring(1), X);
}
var str = "geeksforgeeks" ;
var X = 'e' ;
str = removeCharRecursive(str, X);
document.write(str);
</script>
|
Time Complexity: O(N), as we are using a recursive calls to traverse N times, where N is the length of the string.
Auxiliary Space: O(1), as we are not using any extra space.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...