如何删除一行字符串中的所有*

如题 用调用函数形式为什么我这个不行 我这个只能删除一个 连续的*就只能删除一个

img

你的方法有问题,如果是连续的,s[i]虽然去了,但是他后面的就会继续填补进来,所以只会删除一个。可以用另一个数组来接受,也可以用递归

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void shangchu(char s[], char c)
{
    int i,count=0;
    int len=strlen(s);
    for (i = 0;i<len; i++)
    {
        if (s[i] == c)
        {
            count++;
            for(int j=i;j<len-count;j++)
            {
                s[j]=s[j+1];
            }
            i--;//避免下一个仍为要删去的字符
        }
    }
    s[i-count]='\0';
}
int main()
{
    char a[100], b;
    gets(a);
    scanf("%c", &b);
    shangchu(a, b);
    puts(a);
    system("pause");
    return 0;
}