急求C语言程序,求各位大哥大姐帮帮小妹~!!!谢谢

2024-12-12 16:05:25
推荐回答(2个)
回答1:

/*定义一个函数delete_char,其中包含3个行参,1个字符型,一个字符串型,该函数返回一整数.函数的功是在字符串中删除所以的字符形数据,并删除的个数作为函数的返回值.编写主函数调用该函数.*/
#include
#include
int delete_char(char ch,char str[],int n)
{
int i,t,c=0;
for(i=0,t=0;i if(str[i]==ch) c++;
else str[t++]=str[i];
str[t]='\0';
return c;
}
void main()
{
char str[1024];
char ch;
printf("please input a long string:");
gets(str);
printf("please input a char:");
ch=getchar();
printf("delete char '%c' counts:%d \n",ch,delete_char(ch,str,strlen(str)));
printf("after delete_char:");
puts(str);
}

回答2:

#include
#include

int delete_char(char A,char *B)
{
int i=0,n=0;
while (B[i]!='\0')
{
if(B[i]==A)
{
strcpy(B+i,B+i+1);
n++;
}
else
i++;
}
return n;
}

void main()
{
char a[]="1sdassadfsdsdfgasasdda";
char b='s';
int c=delete_char(b,a);
printf("%s\n",a);
printf("%d",c);
}