查找子串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);
}
测试成功。
如果在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);
}
测试成功。
相关文章推荐
- 【优先队列】HDU1434幸福列车
- soundtouch 变声使用和算法
- 给予 HorizontalListView 自定义banner
- WKWebView、Safari、SFSafariViewController的区别与选取
- Object对象
- if、switch
- 【LeetCode】338. Counting Bits (2 solutions)
- 软件工程课程作业(四)--返回一个整数数组中最大子数组的和
- BZOJ-2154 && 2693 Crash的数字表格&&jzptab 莫比乌斯反演
- 要命的Retrofic框架
- 日期时间转换为世纪秒
- NS_ASSUME_NONNULL_BEGIN,NS_ASSUME_NONNULL_END
- 学习进度表
- python常用包和模块-更新
- 机器学习基础(十三)—— regularization & validation
- python之yield使用
- 软件开发常用的linux命令心得
- 反向代理负载均衡
- C++扬帆远航——19(斐波那契数列第20项)
- sass vs less