- #include .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)
- {
- unsigned int i;
- unsigned int hay_len, need_len;
- const char *p;
-
- if (NULL == haystack || NULL == needle)
- return NULL;
-
-
- 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;
- }
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』