给定一个字符串。编写一个程序来删除字符串中所有出现的字符。
例子:
Input : s = "geeksforgeeks"
c = 'e'
Output : s = "gksforgks"
Input : s = "geeksforgeeks"
c = 'g'
Output : s = "eeksforeeks"
这个想法是维护结果字符串的索引。
C++
// C++ program to remove a particular character
// from a string.
#include
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
// 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
Python3
# Python3 program to remove
# a particular character
# from a string.
# function for removing the
# occurrence of character
def removeChar(s, c) :
# find total no. of
# occurrence 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
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
PHP
输出:
eeksforeeks
时间复杂度: O(n),其中n是输入字符串的长度。
辅助空间: O(1)