编程题:《自定义函数,删除字符串中的字符》
问题描述:删除字符串中的字符。输入一个字符串s,再输入一个字符c,将字符串s中出现的所有字符c删除。要求定义并调用函数delchar(s,c),它的功能是将字符串s中出现的所有c字符删除。
【运行时的输入输出样例】(倾斜处表示输入)
Input a string:happy new year
Input a char:a
After deleted,the string is:hppy new yer
#include<stdio.h>
int main()
{
void delchar(char *x,char y);//调用函数声明
char s[80],c;
printf("Input a string:");
gets(s);
printf("Input a char:");
scanf("%c",&c);
delchar(s,c);
printf("After deleted,the string is:%s",s);
return 0;
}
void delchar(char *x,char y)
{
int i,j;
for(i=0;x[i]!='\0';i++)
{
if(x[i]==y)
{
for(j=i;x[j+1]!='\0';j++)
x[j]=x[j+1];
x[j]='\0';
i--;
}
}
}