c/c++整理--实现删除字符串所有指定字符

编程实现删除字符串中所有指定的字符

示例输入:cabcdefcgchci

示例输出:abdefghi

#include 
#include 
#define N 20

char* deleteChar(char* bef, char c);

int main()
{
	char c, i = 0, j = 0;
	char bef[N] = {0};
	char *aft;
	
	printf("input char: ");
	scanf("%c", &c);
	printf("before delete: ");
	scanf("%s", bef);
	
	aft = deleteChar(bef, c);
	
	printf("after delete: %s\n", aft);
	
	return 0;
}

char* deleteChar(char* bef, char c)
{
	char *aft = (char*)malloc(sizeof(char) * N);
	char *tmp = aft;

	while(*bef)
	{
		if(*bef == c)
		{
			bef++;
			continue;
		}
		*aft = *bef;
		aft++;
		bef++;
	}
	*aft = '\0';
	
	return tmp;
}




你可能感兴趣的:(c/c++细节知识整理)