

题目
解决代码及点评
/*
写strstr函数
简单的遍历去查找吧
*/
#include <iostream>
#include <stdio.h>
const char *my_strstr(const char *str, const char *sub_str)
{
// 遍历
for(int i = 0; str[i] != '\0'; i++)
{
int tem = i; //tem保留主串中的起始判断下标位置
int j = 0;
// 与substr去匹配
while(str[i++] == sub_str[j++])
{
// 如果匹配成功
if(sub_str[j] == '\0')
{
// 返回成功位置
return &str[tem];
}
}
i = tem;
}
return NULL;
}
int main()
{
char *s = "1233345hello";
char *sub = "345";
printf("%s\n", my_strstr(s, sub));
system("pause");
return 0;
}
代码下载及其运行
代码下载地址:http://download.youkuaiyun.com/detail/yincheng01/6704519
解压密码:c.itcast.cn
下载代码并解压后,用VC2013打开interview.sln,并设置对应的启动项目后,点击运行即可,具体步骤如下:
1)设置启动项目:右键点击解决方案,在弹出菜单中选择“设置启动项目”
2)在下拉框中选择相应项目,项目名和博客编号一致
3)点击“本地Windows调试器”运行
程序运行结果