The Wayback Machine - https://web.archive.org/web/20240807225023/https://www.geeksforgeeks.org/remove-all-occurrences-of-a-character-in-a-string-recursive-approach/
Open In App

Remove all occurrences of a character in a string | Recursive approach

Last Updated : 31 May, 2022
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

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: 
 

  1. Get the string str and character X for which character X is to be removed.
  2. 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++




// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to remove all occurrences
// of a character in the string
string removeCharRecursive(string str,
                           char X)
{
    // Base Case
    if (str.length() == 0) {
        return "";
    }
 
    // Check the first character
    // of the given string
    if (str[0] == X) {
 
        // Pass the rest of the string
        // to recursion Function call
        return removeCharRecursive(str.substr(1), X);
    }
 
    // Add the first character of str
    // and string from recursion
    return str[0]
           + removeCharRecursive(str.substr(1), X);
}
 
// Driver Code
int main()
{
    // Given String
    string str = "geeksforgeeks";
 
    // Given character
    char X = 'e';
 
    // Function Call
    str = removeCharRecursive(str, X);
    cout << str;
    return 0;
}


Java




// Java program for the above approach
class GFG{
     
// Function to remove all occurrences
// of a character in the string
static String removeCharRecursive(String str,
                                  char X)
{
     
    // Base Case
    if (str.length() == 0)
    {
        return "";
    }
 
    // Check the first character
    // of the given string
    if (str.charAt(0) == X)
    {
 
        // Pass the rest of the string
        // to recursion Function call
        return removeCharRecursive(
               str.substring(1), X);
    }
 
    // Add the first character of str
    // and string from recursion
    return str.charAt(0) +
           removeCharRecursive(
           str.substring(1), X);
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given String
    String str = "geeksforgeeks";
 
    // Given character
    char X = 'e';
 
    // Function call
    str = removeCharRecursive(str, X);
     
    System.out.println(str);
}
}
 
// This code is contributed by jrishabh99


Python3




# Python3 program for the above approach
 
# Function to remove all occurrences
# of a character in the string
def removeCharRecursive(str, X):
     
    # Base Case
    if (len(str) == 0):
        return ""
     
    # Check the first character
    # of the given string
    if (str[0] == X):
 
        # Pass the rest of the string
        # to recursion Function call
        return removeCharRecursive(str[1:], X)
     
    # Add the first character of str
    # and string from recursion
    return str[0] + removeCharRecursive(str[1:], X)
 
# Driver Code
 
# Given String
str = "geeksforgeeks"
 
# Given character
X = 'e'
 
# Function call
str = removeCharRecursive(str, X)
 
print(str)
 
# This code is contributed by sanjoy_62


C#




// C# program for the above approach
using System;
 
class GFG{
     
// Function to remove all occurrences
// of a character in the string
static String removeCharRecursive(String str,
                                  char X)
{
     
    // Base Case
    if (str.Length == 0)
    {
        return "";
    }
 
    // Check the first character
    // of the given string
    if (str[0] == X)
    {
 
        // Pass the rest of the string
        // to recursion Function call
        return removeCharRecursive(
            str.Substring(1), X);
    }
 
    // Add the first character of str
    // and string from recursion
    return str[0] + removeCharRecursive(
                    str.Substring(1), X);
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given String
    String str = "geeksforgeeks";
 
    // Given character
    char X = 'e';
 
    // Function call
    str = removeCharRecursive(str, X);
     
    Console.WriteLine(str);
}
}
 
// This code is contributed by Amit Katiyar


Javascript




<script>
 
// javascript program for the above approach
 
     
// Function to remove all occurrences
// of a character in the string
function removeCharRecursive(str,X)
{
     
    // Base Case
    if (str.length == 0)
    {
        return "";
    }
 
    // Check the first character
    // of the given string
    if (str.charAt(0) == X)
    {
 
        // Pass the rest of the string
        // to recursion Function call
        return removeCharRecursive(
               str.substring(1), X);
    }
 
    // Add the first character of str
    // and string from recursion
    return str.charAt(0) +
           removeCharRecursive(
           str.substring(1), X);
}
 
// Driver Code
//Given String
var str = "geeksforgeeks";
 
// Given character
var X = 'e';
 
// Function call
str = removeCharRecursive(str, X);
 
document.write(str);
 
// This code is contributed by 29AjayKumar
</script>


Output: 

gksforgks

 

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.
 



Similar Reads

Remove all continuous occurrences of 'a' and all occurrences of 'b'
Given a string str, the task is to remove all the continuous occurrences of a and all occurrences of b and print the resultant string. Examples Input: str = "abcddabcddddabbbaaaaaa" Output: acddacdddda 'abcddabcddddabbbaaaaaa' will not result in 'acddacddddaa' because after removing the required occurrences, the string will become 'acddacddddaa' wh
10 min read
Map every character of one string to another such that all occurrences are mapped to the same character
Given two strings s1 and s2, the task is to check if characters of the first string can be mapped with the character of the second string such that if a character ch1 is mapped with some character ch2 then all the occurrences of ch1 will only be mapped with ch2 for both the strings. Examples: Input: s1 = "axx", s2 = "cbc" Output: Yes 'a' in s1 can
8 min read
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
7 min read
Remove all occurrences of a character from a string using STL
Given a string S and a character C, the task is to remove all the occurrences of the character C from the given string. Examples: Input:vS = "GFG IS FUN", C = 'F' Output:GG IS UN Explanation: Removing all occurrences of the character 'F' modifies S to "GG IS UN". Therefore, the required output is GG IS UN Input: S = "PLEASE REMOVE THE SPACES", C =
2 min read
Remove all occurrences of a character in a string
Given a string. Write a program to remove all the occurrences of a character in the string. Examples: Input : s = "geeksforgeeks" c = 'e'Output : s = "gksforgks"Input : s = "geeksforgeeks" c = 'g'Output : s = "eeksforeeks" Input : s = "geeksforgeeks" c = 'k'Output : s = "geesforgees"First Approach: The idea is to maintain an index of the resultant
10 min read
Count all sub-sequences having product &lt;= K - Recursive approach
Given an integer K and a non-negative array arr[], the task is to find the number of sub-sequences having the product ? K. This problem already has a dynamic programming solution. This solution aims to provide an optimized recursive strategy to the problem. Examples: Input: arr[] = { 1, 2, 3, 4 }, K = 10 Output: 11 The sub-sequences are {1}, {2}, {
13 min read
Most frequent character in a string after replacing all occurrences of X in a Binary String
Given a string S of length N consisting of 1, 0, and X, the task is to print the character ('1' or '0') with the maximum frequency after replacing every occurrence of X as per the following conditions: If the character present adjacently to the left of X is 1, replace X with 1.If the character present adjacently to the right of X is 0, replace X wi
15+ min read
Remove all occurrences of a string t in string s using Boyer-Moore Algorithm
Given a string s and string t, the task is to remove all occurrences of a string t in a string s using the Boyer-Moore algorithm. Examples: Input: s = "ababaababa", t = "aba" Output: baab Input: s = "Geeksforgeeks", t = "eek"Output: Gsforgs Approach: This can be solved with the following idea: We initialize the bad character rule and then loop thro
10 min read
Remove all occurrences of string t in string s using KMP Algorithm
Given two strings s and t, the task is to remove all occurrences of t in s and return the modified string s, and you have to use the KMP algorithm to solve this. Examples: Input: s = "abcdefgabcabcabdefghabc", t = "abc"Output: "defgdefgh" Input: s = "aaabbbccc", t = "bbb"Output: "aaaccc" Approach: To solve the problem follow the below idea: We will
8 min read
Number of permutations of a string in which all the occurrences of a given character occurs together
Given a string 's' and a character 'c', the task is to find the number of permutations of the string in which all the occurrences of the character 'c' will be together (one after another). Examples : Input: Str = "AKA" ch = 'A' Output: 2 Explanation: All the unique permutations of AKA are : AKA, AAK and KAA 'A' comes consecutively only twice. Hence
9 min read
three90RightbarBannerImg