vs2013
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<assert.h>
char* my_strstr(const char* p1,const char* p2)
{
assert(p1 != NULL);
assert(p2 != NULL);
char* s1 = (char*)p1;
char* s2 = (char*)p2;
char* str = (char*)p1;
if (!*p2 )
{
return (char*)p1;
}
while (*str)
{
s1 = str;
s2 = (char*)p2;
while (*s1 && *s2 && !(*s1 - *s2))
{
s1++;
s2++;
}
if (!*s2)
{
return str;
}
str++;
}
return NULL;
}
int main()
{
char* p1 = "abcde";
char* p2 = "bc";
char* ret = my_strstr(p1, p2);
if (ret != NULL)
{
printf("找到了\n");
printf("%s\n", ret);
}
else
printf("不存在");
return 0;
}
//条件 *str 相当于 *str!='\0';
//条件 *s1==*s2 相当于 !(*s1-*s2)