1. 编制函数 fun,其功能是:删除一个字符串中指定的字符。

2025-01-01 11:52:25
推荐回答(2个)
回答1:

#include
#include
#include
void fun(char* src, char ch)
{
int len=strlen(src);
char *temp=(char *)malloc(len+1);
strcpy(temp,src);
while(*temp!='\0')
{
if(*temp != ch)
{
*src = *temp;
src++;
}
temp++;
}
*src='\0';
free(temp);
}
int main()
{
char *src="This is an example!";
char ch='e';
printf("before funed, the string is:%s\n",src);
fun(src, ch);
printf("after funed, the string is:%s\n",src);
system("pause");
return 0;
}

回答2:

void fun(char* str, char chr)
{
char *p, *q;
p = q = str;
while(*p != '\0')
{
if(*p != chr)
*q++ = *p++;
else
p++;
}
*q = '\0';
}