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.
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[] = "geeksforgeeks" ;
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 = "geeksforgeeks" ;
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 = "geeksforgeeks"
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 = "geeksforgeeks" ;
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 = "geeksforgeeks" ;
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 = "geeksforgeeks" ;
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= "geeksforgeeks" ;
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 = "geeksforgeeks" ;
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 = "geeksforgeeks"
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 = "geeksforgeeks" ;
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= "geeksforgeeks" ;
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).
Please Login to comment...