Given a string. Write a program to remove all the occurrences of a character in the string.
Examples:
Input : s = "neveropen"
c = 'e'
Output : s = "gksforgks"
Input : s = "neveropen"
c = 'g'
Output : s = "eeksforeeks"
Input : s = "neveropen"
c = 'k'
Output : s = "geesforgees"
First Approach: The idea is to maintain an index of the resultant string.
Implementation:
C++
#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[] = "neveropen" ;
removeChar(s, 'g' );
cout << s;
return 0;
}
|
Java
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);
}
public static void main(String[] args)
{
String s = "neveropen" ;
removeChar(s, 'g' );
}
}
|
Python3
def removeChar(s, c) :
counts = s.count(c)
s = list (s)
while counts :
s.remove(c)
counts - = 1
s = '' . join(s)
print (s)
if __name__ = = '__main__' :
s = "neveropen"
removeChar(s, 'g' )
|
C#
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);
}
public static void Main()
{
string s = "neveropen" ;
removeChar(s, 'g' );
}
}
|
PHP
<?php
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 ;
}
$s = "neveropen" ;
removeChar( $s , 'g' );
?>
|
Javascript
<script>
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( "" ));
}
let s = "neveropen" ;
removeChar(s, 'g' );
</script>
|
Complexity Analysis:
- 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()).
Implementation:
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;
}
int main()
{
string word= "neveropen" ;
char ch= 'e' ;
cout<<removechar(word,ch);
return 0;
}
|
Java
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();
}
public static void main(String args[])
{
String word = "neveropen" ;
char ch = 'e' ;
System.out.println(removechar(word, ch));
}
}
|
Python3
def removechar(word, ch):
i = 0
while (i < len (word)):
if (word[i] = = ch):
word = word[:i] + word[i + 1 :]
i - = 1
i + = 1
return word
word = "neveropen"
ch = 'e'
print (removechar(word,ch))
|
C#
using System;
using System.Text;
using System.Collections;
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[i] == ch) {
s.Remove(i, 1);
i--;
}
}
return s.ToString();
}
public static void Main()
{
string word = "neveropen" ;
char ch = 'e' ;
Console.WriteLine(removechar(word, ch));
}
}
|
Javascript
function removechar(word, ch)
{
let ans= "" ;
for (let i=0;i<word.length;i++)
{
if (word[i]!=ch){
ans+=word[i];
}
}
return ans;
}
let word= "neveropen" ;
let ch='e';
document.write(removechar(word,ch));
|
Complexity Analysis:
- Time Complexity: O(n), where n is length of input string.
- Auxiliary Space: O(1).
Third approach : Using built-in replace() function in Python and Javascript.
Implementation:
C++
#include <iostream>
#include <string>
using namespace std;
void removeChar(string& word, char ch) {
size_t found = word.find(ch);
while (found != string::npos) {
word.erase(found, 1);
found = word.find(ch, found);
}
cout << word << endl;
}
int main() {
string word = "neveropen" ;
removeChar(word, 'k' );
return 0;
}
|
Java
public class Main {
public static void removeChar(String word, char ch) {
word = word.replace(Character.toString(ch), "" );
System.out.println(word);
}
public static void main(String[] args) {
String word = "neveropen" ;
removeChar(word, 'k' );
}
}
|
Python3
def removeChar(word, ch) :
word = word.replace(ch,'')
print (word)
if __name__ = = '__main__' :
word = "neveropen"
removeChar(word, 'k' )
|
C#
using System;
class Program {
static void removeChar( string word, char ch)
{
word = word.Replace(ch.ToString(), "" );
Console.WriteLine(word);
}
static void Main( string [] args)
{
string word = "neveropen" ;
removeChar(word, 'k' );
}
}
|
Javascript
var word = "neveropen" ;
var c =/k/g ;
word = word.replace(c, '' );
console.log(word);
|
Complexity Analysis:
- Time Complexity: O(n), where n is the length of the input string.
- Auxiliary Space: O(1).
Fourth Approach: Using Recursion
Here our recursive function’s base case will be when string length’s become 0. And in the recursive function, if the encountered character is the character that we have to remove then call the recursive function from the next character else store this character in answer and then call the recursive function from the next character.
Code:
C++
#include <bits/stdc++.h>
using namespace std;
string removeChar(string str, char ch)
{
if (str.length() == 0) {
return "" ;
}
if (str[0] == ch) {
return removeChar(str.substr(1), ch);
}
return str[0] + removeChar(str.substr(1), ch);
}
int main()
{
string str = "neveropen" ;
str = removeChar(str, 'g' );
cout << str;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class Main {
static String removeChar(String str, char ch)
{
if (str.length() == 0 ) {
return "" ;
}
if (str.charAt( 0 ) == ch) {
return removeChar(str.substring( 1 ), ch);
}
return str.charAt( 0 )
+ removeChar(str.substring( 1 ), ch);
}
public static void main(String[] args)
{
String str = "neveropen" ;
str = removeChar(str, 'g' );
System.out.println(str);
}
}
|
Python3
def removeChar( str , ch):
if len ( str ) = = 0 :
return ""
if str [ 0 ] = = ch:
return removeChar( str [ 1 :], ch)
return str [ 0 ] + removeChar( str [ 1 :], ch)
if __name__ = = '__main__' :
str = "neveropen"
str = removeChar( str , 'g' )
print ( str )
|
C#
using System;
class MainClass {
public static string RemoveChar( string str, char ch)
{
if (str.Length == 0) {
return "" ;
}
if (str[0] == ch) {
return RemoveChar(str.Substring(1), ch);
}
return str[0] + RemoveChar(str.Substring(1), ch);
}
public static void Main( string [] args)
{
string str = "neveropen" ;
str = RemoveChar(str, 'g' );
Console.WriteLine(str);
}
}
|
Javascript
function removeChar(str, ch) {
if (str.length == 0) {
return "" ;
}
if (str[0] == ch) {
return removeChar(str.slice(1), ch);
}
return str[0] + removeChar(str.slice(1), ch);
}
let str = "neveropen" ;
str = removeChar(str, 'g' );
console.log(str);
|
Complexity Analysis-
- Time Complexity: O(n), where n is the length of the input string.
- Auxiliary Space: O(n),if we consider recursion stack space
Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!