Remove all occurrences of a character in a string

22 Apr, 2022
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"
First Approach: The idea is to maintain an index of the resultant string.  
C++
Java
Python3
C#
PHP
Javascript
// C++ program to remove a particular character// from a string.#include <bits/stdc++.h>using namespace std; void removeChar(char* s, char c){     int j, n = strlen(s);    for (int i = j = 0; i < n; i++)        if (s[i] != c)            s[j++] = s[i];     s[j] = '\0';} int main(){    char s[] = "geeksforgeeks";    removeChar(s, 'g');    cout << s;    return 0;}
// Java program to remove// a particular character// from a string.class GFG{static void removeChar(String s, char c){    int j, count = 0, n = s.length();    char []t = s.toCharArray();    for (int i = j = 0; i < n; i++)    {        if (t[i] != c)        t[j++] = t[i];        else            count++;    }         while(count > 0)    {        t[j++] = '\0';        count--;    }         System.out.println(t);} // Driver Codepublic static void main(String[] args){    String s = "geeksforgeeks";    removeChar(s, 'g');}} // This code is contributed// by ChitraNayal
# Python3 program to remove# a particular character# from a string. # function for removing the# occurrence of characterdef removeChar(s, c) :         # find total no. of    # occurrence of character    counts = s.count(c)     # convert into list    # of characters    s = list(s)     # keep looping until    # counts become 0    while counts :                 # remove character        # from the list        s.remove(c)         # decremented by one        counts -= 1     # join all remaining characters    # of the list with empty string    s = '' . join(s)         print(s) # Driver codeif __name__ == '__main__' :         s = "geeksforgeeks"    removeChar(s,'g')     # This code is contributed# by Ankit Rai
// C# program to remove a// particular character// from a string.using System; class GFG{static void removeChar(string s,                       char c){    int j, count = 0, n = s.Length;    char[] t = s.ToCharArray();    for (int i = j = 0; i < n; i++)    {        if (s[i] != c)        t[j++] = s[i];        else            count++;    }         while(count > 0)    {        t[j++] = '\0';        count--;    }         Console.Write(t);} // Driver Codepublic static void Main(){    string s = "geeksforgeeks";    removeChar(s, 'g');}} // This code is contributed// by ChitraNayal
<?php// PHP program to remove a// particular character// from a string. function removeChar($s, $c){     $n = strlen($s);    $count = 0;    for ($i = $j = 0; $i < $n; $i++)    {    if ($s[$i] != $c)        $s[$j++] = $s[$i];    else        $count++;    }    while($count--)    {        $s[$j++] = NULL;    }    echo $s;} // Driver code$s = "geeksforgeeks";removeChar($s, 'g'); // This code is contributed// by ChitraNayal?>
<script> // Javascript program to remove// a particular character// from a string.function removeChar(s, c){    let j, count = 0, n = s.length;    let t = s.split("");         for(let i = j = 0; i < n; i++)    {        if (t[i] != c)            t[j++] = t[i];        else            count++;    }          while (count > 0)    {        t[j++] = '\0';        count--;    }    document.write(t.join(""));} // Driver Codelet s = "geeksforgeeks";removeChar(s, 'g'); // This code is contributed by avanitrachhadiya2155 </script>
eeksforeeks
 
Time Complexity : O(n) where n is length of input string. Auxiliary Space : O(1)
Second Approach in C++: We can also use the STL string class and erase function to delete any character at any position using the base addressing (string.begin()).
C++14
#include <bits/stdc++.h>using namespace std; string removechar(string& word,char& ch){    for(int i=0;i<word.length();i++)    {        if(word[i]==ch){        word.erase(word.begin()+i);        i--;        }    }         return word;}  // driver's codeint main(){    string word="geeksforgeeks";    char ch='e';    cout<<removechar(word,ch);     return 0;}
Time Complexity: O(n), where n is length of input string. 
Auxiliary Space: O(1)
ankthon
ukasp
ManasChhabra2
nidhi_biet
shruti371sg
avanitrachhadiya2155
adnanirshad158
gabaa406
prophet1999
Searching
Strings
Searching
Strings
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Search, insert and delete in an unsorted array
Median of two sorted arrays of different sizes
k largest(or smallest) elements in an array
Two Pointers Technique
Search, insert and delete in a sorted array
Write a program to reverse an array or string
Reverse a string in Java
Write a program to print all permutations of a given string
C++ Data Types
Different Methods to Reverse a String in C++