模拟实现strstr
#include<stdio.h>
#include<assert.h>
//my_strstr
char* my_strstr(const char *string, const char *strCharSet)
{
assert(string != NULL && strCharSet != NULL);
const char *s = string; //主串
const char *t = strCharSet; //模式串
int i, j;
i = j = 0;
while (s[i] != '\0' && t[j] != '\0')
{
if (s[i] == t[j])
{
i++;
j++;
}
else
{
i = i - j + 1; //关键
j = 0;
}
}
if (t[j] == '\0')
return s + i - j;
return NULL;
}
void main()
{
char *str = "abababcabababc";
char *t = "abc";
char *res = my_strstr(str, t);
if (res == NULL)
printf("不存在.\n");
else
printf("%s\n", res);
}