【问题描述】
删除字符串中的字符。输入一个字符串s,再输入一个字符c,将字符串s中出现的所有字符c删除。要求定义并调用函数delchar(s,c),它的功能是将字符串s中出现的所有c字符删除。
【输入形式】
首先打印提示“Input a string:";然后直接在冒号后面输入字符串,字符串中可以包含空格;字符串以回车结束。
打印提示“Input a char:”;然后直接在冒号后面输入一个字符;回车。
【输出形式】
首先打印“After deleted,the string is:”;紧跟后面输出被删除后的字符串剩余内容;换行。
【运行时的输入输出样例】(下划线部分表示输入)
Input a string:happy new year
Input a char:a
After deleted,the string is:hppy new yer
#include <stdio.h>
#include <string.h>
int main ()
{
printf("Input a String:");
void fun(char *a,char c);
char b[20],d;
gets(b);
printf("Input a char:");
scanf("%c",&d);
fun(b,d);
printf("After deleted,the string is:");
puts(b);
return 0;
}
void fun(char *a,char c)
{
int i,m,n=0;
m=strlen(a);
for (i=0;i<m;i++)
{
if(*(a+i)!=c)
*(a+(n++))=*(a+i);
}
*(a+n)='\0';
}