#include <asssert.h>
//kMP算法
char* my_strstr(const char* p1, const char* p2)
{
assert(p1 != NULL);
assert(p2 != NULL);
char *s1 = (char *)p1;
char *s2 = (char *)p2;
char *cur = (char *)p1;
if (!*p2)
{
return (char *)p1;
}
while (*cur)
{
s1 = cur;
s2 = (char *)p2;
while(*s1 && *s2 && !(*s1 - *p2))
{
s1++;
s2++;
}
if (!*s2)
{
return cur;//找到字符串
}
if (*s1 == '\0')
{
return NULL;
}
cur++;
}
return NULL;//找不到字符串
}
int main()
{
char *p1 = "abbbcdef";
char *p2 = "bbc";
char* ret = my_strstr(p1, p2);
if (ret == NULL)
{
printf("字串不存在\n");
}
else
{
printf("%s\n", ret);
}
return 0;
}