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"
The idea is to maintain an index of resultant string.
C++
// C++ program to remove a particular character // from a string. #include <bits/stdc++.h> using namespace std; void removeChar(char *s, int 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; } |
chevron_right
filter_none
Java
// 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 Code public static void main(String[] args) { String s = "geeksforgeeks"; removeChar(s, 'g'); } } // This code is contributed // by ChitraNayal |
chevron_right
filter_none
Python3
# Python3 program to remove # a particular character # from a string. # function for removing the # occurence of character def removeChar(s, c) : # find total no. of # occurence of character counts = s.count(c) # convert into list # of characters s = list(s) # keeep looping untill # 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 code if __name__ == '__main__' : s = "geeksforgeeks" removeChar(s,'g') # This code is contributed # by Ankit Rai |
chevron_right
filter_none
C#
// 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 Code public static void Main() { string s = "geeksforgeeks"; removeChar(s, 'g'); } } // This code is contributed // by ChitraNayal |
chevron_right
filter_none
PHP
<?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 ?> |
chevron_right
filter_none
Output:
eeksforeeks
Time Complexity : O(n) where n is length of input string.
Auxiliary Space : O(1)
Recommended Posts:
- Map every character of one string to another such that all occurrences are mapped to the same character
- Count occurrences of a character in a repeated string
- Print the string by ignoring alternate occurrences of any character
- Number of permutations of a string in which all the occurrences of a given character occurs together
- Remove the first and last character of each word in a string
- Remove a character from a string to make it a palindrome
- Check if all occurrences of a character appear together
- Remove all occurrences of any element for maximum array sum
- Minimize ASCII values sum after removing all occurrences of one character
- Find the number of occurrences of a character upto preceding position
- Find a string such that every character is lexicographically greater than its immediate next character
- Replace every character of string by character whose ASCII value is K times more than it
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Count occurrences of a string that can be constructed from another given string
- Replace every character of a string by a different character
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



