从字符串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
本文出自 “痕迹” 博客,请务必保留此出处http://wpfbcr.blog.51cto.com/10696766/1707266