## 【我解C语言面试题系列】011 删除指定字符串的相应字符
**删除指定字符串的相应字符**
假设字符串"cabcdefcgchci" ,那么要求你写一个函数,把该字符串中所有的字符 ’c’ 删除掉。那么结果应该是 "abdefghi"。
**00和01是用for循环来实现的算法**
~~~
char * DeleteChararcter_00(char *str,int c)
{
char *p,*ret;
for(ret= p = str;*p;p++)
{
if(*p == c)
continue;
*str++ = *p;
}
*str = '/0';
return ret;
}
char * DeleteChararcter_01(char *str,int c)
{
char *p,*ret;
for(ret= p = str;*p;p++)
{
if(*p != c)
*str++ = *p;
}
*str = '/0';
return ret;
}
~~~
**02和03是用while循环来实现的算法**
~~~
char * DeleteChararcter_02(char *str,int c)
{
char *p,*ret;
ret = p = str;
while(*p)
{
if(*p++ == c)
continue;
*str++ = *(p-1);
}
*str = '/0';
return ret;
}
char * DeleteChararcter_03(char *str,int c)
{
char *p,*ret;
ret = p = str;
while(*p)
{
if(*p != c)
*str++ = *p;
p++;
}
*str = '/0';
return ret;
}
~~~