其他分享
首页 > 其他分享> > 删除字符串中,字符 c 出现所有的字符

删除字符串中,字符 c 出现所有的字符

作者:互联网

原文链接:https://www.geeksforgeeks.org/remove-all-occurrences-of-a-character-in-a-string/

Remove all occurrences of a character in a string 

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"
// 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 

 

标签:count,字符,string,删除,++,geeksforgeeks,character,occurrences,字符串
来源: https://blog.csdn.net/qq_28632639/article/details/100701814