Related Articles

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

• Difficulty Level : Medium
• Last Updated : 19 May, 2021

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 == 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 + recursive_function(str.substr(1), X)`

Below is the implementation of the above approach:

## C++

 `// C++ program for the above approach``#include ``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 == 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``           ``+ 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 == 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 + 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

 ``
Output:
`gksforgks`

Time Complexity: O(N), where N is the length of the string
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up