VOOZH about

URL: https://www.geeksforgeeks.org/dsa/remove-all-occurrences-of-a-character-in-a-string/

⇱ Remove all occurrences of a character in a string - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Remove all occurrences of a character in a string

Last Updated : 27 Feb, 2026

Given a string and a character, remove all the occurrences of the 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"

[Approach 1] Using Built-In Method - O(n) Time and O(1) Space

This approach uses the built-in Replace() function. It directly replaces all occurrences of the given character with an empty string (""), effectively removing them from the original string.


Output
bbc

[Approach 2] Writing Your Own Method - O(n) Time and O(1) Space

This approach iterates through the string and retains only the characters that are not equal to the given character by shifting them toward the beginning of the string. After processing all characters, the string is resized to remove the leftover characters at the end.


Output
eeksforeeks
Comment
Article Tags:
Article Tags: