字符串倒序查找字串
- #include.h>
- #include<string.h>
- char*myStrrstr(constchar*haystack,constchar*needle);
- int
- main(void)
- {
char*s="hello world"; char*t="ll"; char*r=myStrrstr(s,t); printf(r); return 0; - }
- char*myStrrstr(constchar*haystack,constchar*needle)
- {
unsigned int i; unsigned int hay_len,need_len; constchar*p; if(NULL==haystack||NULL==needle) returnNULL; hay_len=strlen(haystack); need_len=strlen(needle); if(need_len==0) return(char*)haystack; if(hay_len< need_len) returnNULL; p=haystack+hay_len-need_len; while(p>=haystack) { for(i=0;i< need_len;i++) if(p[i]!=needle[i]) gotonext; return(char*)p; next: p--; } returnNULL; - }
評(píng)論