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"
Table of Content
[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.
#include <algorithm>
#include <iostream>
using namespace std;
int main() {
string s = "ababca";
char c = 'a';
// Remove all occurrences of 'c' from 's'
s.erase(remove(s.begin(), s.end(), c), s.end());
cout << s;
return 0;
}
import java.util.*;
public class Gfg {
public static void main(String[] args) {
String s = "ababca";
char c = 'a';
// Remove all occurrences of 'c' from 's'
s = s.replace(String.valueOf(c), "");
System.out.println(s);
}
}
def main():
s = "ababca"
c = 'a'
# Remove all occurrences of 'c' from 's'
s = s.replace(c, '')
print(s)
if __name__ == "__main__":
main()
using System;
class Gfg {
static void Main() {
string s = "ababca";
char c = 'a';
// Remove all occurrences of 'c' from 's'
s = s.Replace(c.ToString(), "");
Console.WriteLine(s);
}
}
let s = "ababca";
let c = 'a';
// Remove all occurrences of 'c' from 's'
s = s.split(c).join('');
console.log(s);
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.
#include <iostream>
using namespace std;
void removeChar(string &s, char c) {
int j = 0;
for (int i = 0; i < s.size(); i++) {
if (s[i] != c) {
s[j++] = s[i];
}
}
// Resize string to remove the
// extra characters
s.resize(j);
}
int main() {
string s = "geeksforgeeks";
removeChar(s, 'g');
cout << s;
return 0;
}
import java.util.*;
public class GFG {
public static String removechar(String word, char ch)
{
StringBuilder s = new StringBuilder(word);
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == ch) {
s.deleteCharAt(i);
i--;
}
}
return s.toString();
}
// driver's code
public static void main(String args[])
{
String word = "geeksforgeeks";
char ch = 'e';
System.out.println(removechar(word, ch));
}
}
def removeChar(s, c):
j = 0
s_list = list(s)
for i in range(len(s_list)):
if s_list[i] != c:
s_list[j] = s_list[i]
j += 1
# Resize string to remove the
# extra characters
return ''.join(s_list[:j])
def main():
s = "geeksforgeeks"
s = removeChar(s, 'g')
print(s)
if __name__ == "__main__":
main()
using System;
class GFG {
static void removeChar(ref string s, char c) {
int j = 0;
char[] arr = s.ToCharArray();
for (int i = 0; i < arr.Length; i++) {
if (arr[i] != c) {
arr[j++] = arr[i];
}
}
// Resize string to remove the
// extra characters
s = new string(arr, 0, j);
}
static void Main() {
string s = "geeksforgeeks";
removeChar(ref s, 'g');
Console.WriteLine(s);
}
}
function removeChar(s, c) {
let arr = s.split('');
let j = 0;
for (let i = 0; i < arr.length; i++) {
if (arr[i] !== c) {
arr[j++] = arr[i];
}
}
// Resize string to remove the
// extra characters
return arr.slice(0, j).join('');
}
//Driver Code
function main() {
let s = "geeksforgeeks";
s = removeChar(s, 'g');
console.log(s);
}
main();
Output
eeksforeeks