从字符串str"hello-NOworld"中删除字符串sub"NO"
如果找到并成功删除返回1,不然那返回0。
#include<stdio.h>
#include<assert.h>
int my_substr(char *str,const char*sub)
{
assert(sub);
char *cp1_str = str;
char *cp2_str = str;
const char *cp_sub = sub;
while (*str)
{
cp1_str = str;
while (*sub)
{
if (*sub == *cp1_str)
{
sub++;
cp1_str++;
}
else
{
break;
}
}
if (*sub == '\0')
{
while (*cp1_str)
{
*str = *cp1_str;
cp1_str++;
str++;
}
*str = '\0';
return 1;
}
sub = cp_sub;
str++;
}
return 0;
}
int main()
{
char str[] = "hello-NOworld";
char *chars = "NO";
int ret = my_substr(str, chars);
printf("str=%s,ret=%d", str, ret);
}
输出结果为,str=hello-world,ret=1
转载于:https://blog.51cto.com/wpfbcr/1707266