- #include <stdio.h>
-
#include <string.h>
-
-
char *myStrstr(const char *haystack, const char *needle);
-
-
int
-
main(void)
-
{
-
char *s = "Hello world";
-
char *t = "ll";
-
char *r = myStrstr(s, t);
-
printf(r);
-
-
return 0;
-
}
-
-
char *
-
myStrstr(const char *haystack, const char *needle)
-
{
-
if (NULL == haystack || NULL == needle)
-
return NULL;
-
-
size_t i;
-
size_t hay_len, need_len;
-
const char *p = haystack;
-
-
hay_len = strlen(haystack);
-
need_len = strlen(needle);
-
-
if (need_len == 0)
-
return (char *)haystack;
-
-
if (hay_len < need_len)
-
return NULL;
-
-
while (p <= haystack + hay_len - need_len) {
-
for (i = 0; i < need_len; i++)
-
if (p[i] != needle[i])
-
goto next;
-
return (char *)p;
-
-
next:
-
p++;
-
}
-
-
return NULL;
- }