strindex实现
2016-02-01 17:03
106 查看
//库函数中没有这个,需自己实现
//s:源字符串
//t:目标查找字符串
//返回 t在s中起始的偏移,若超找失败返回-1
#inclue <string.h>
int strindex(const char *s, const char *t)//若要提高效率可先查找字符串头字符(使用折半查找法等)
{
int i = 0;
int sLen = 0;
int tLen = 0;
if((s == NULL) || (t == NULL))
return (-1);
sLen = strlen(s);
tLen = strlen(t);
if(sLen < tLen)
return (-1);
for (; i <= sLen - tLen ; i++)
if (strncmp(s + i, t, tLen) == 0)
return (i);
return (-1);
}
//s:源字符串
//t:目标查找字符串
//返回 t在s中起始的偏移,若超找失败返回-1
#inclue <string.h>
int strindex(const char *s, const char *t)//若要提高效率可先查找字符串头字符(使用折半查找法等)
{
int i = 0;
int sLen = 0;
int tLen = 0;
if((s == NULL) || (t == NULL))
return (-1);
sLen = strlen(s);
tLen = strlen(t);
if(sLen < tLen)
return (-1);
for (; i <= sLen - tLen ; i++)
if (strncmp(s + i, t, tLen) == 0)
return (i);
return (-1);
}
相关文章推荐
- fir.im Weekly - 技术人也要苦练“七十二变”
- Android Fragment是什么
- 游戏中接入第三方渠道时 关于meta-data的一些小问题
- 网狐 自绘 倒计时和准备按钮
- Java实现排队论的原理
- VNC Server Configuration
- POJ3461 字符串hash
- python 几种常见的测试框架
- iOS开发-NSInteger 和 int 的区别
- 加载动作
- MongoDB的安装以及自启动问题
- OI知识体系
- 多线程之NSThread
- 一个轻client,多语言支持,去中心化,自己主动负载,可扩展的实时数据写服务的实现方案讨论
- 如果数据文件顺序被打乱,你有办法找回正确的文件把数据库打开吗?
- Bool
- AS不能发布release版本的解决方案
- mongodb---第一天
- 在OPENSTACK建VM时候,设置采用SSH密码认证登录
- 设计模式之_简单工厂模式、工厂方法模式、抽象工厂模式 、策略模式、策略与工厂的区别(转)