您的位置:首页 > 其它

查找子串substr()函数的实现——循环

2016-03-24 19:39 316 查看
实现查找子串的substr(char *s1, char *s2)函数。

如果在s1中找到了s2,就返回位置;否则返回-1。

int hjd_substr(char *s1, char *s2)
{
int nResult = -1;
int i=0, j=0;
while ((*(s1+i)!='\0')&&(*(s2+j)!='\0'))
{
if(*(s1+i)==*(s2+j))
{
i++;
j++;
}
else
{
i++;
j=0;
}
}
if(*(s2+j)=='\0')
nResult = i-j;
else
nResult=-1;
return(nResult);
}

测试成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: