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