C实例编程(删除子字符串的程序)
一个删除子串的程序,不使用C的相关字符串库函数:
# include
intdel _ substr(char * str,char const * substr);
int my _ strlen(char const * str);
int main(void)
{
char str[]= " abcdefgh ";
char * substr 1 = " EFG ";
char * substr 2 = " fgr ";
printf("%d\t%s\n ",del_substr(str,substr1),str);
printf("%d\t%s\n ",del_substr(str,substr2),str);
返回0;
}
/* *
* * str-string
* * substr-substring
* *如果成功找到并删除了子字符串,测试提示将返回1,否则返回0
*
char * temp _ str = str;
char const * temp _ substr = substr;
int sub _ len = my _ strlen(substr);
while(1)
{
if(* temp _ str = = * temp _ substr)
{
temp _ substr++;
if(* temp _ substr = = ' \ 0 ')
{
break;
}
}
else
{
temp _ substr = substr;
}
if(* temp _ str = = ' \ 0 ')
{
return 0;
}
temp _ str++;
}
char * preDel = temp _ str-(sub _ len-1);
while(*(preDel+(I+sub _ len))!= ' \ 0 ')
{
*(predl+I)= *(preDel+(I+sub _ len));
i++;
}
*(preDel+I)= ' \ 0 ';
return 1;
}
int my _ strlen(char const * str)
{
int len = 0;
while( *str++!= ' \ 0 ')
{
len++;
}
return len;
}
0条评论