×
嵌入式 > 技术百科 > 详情

字符串倒序查找字串

发布时间:2020-09-14 发布时间:
|
  1. #include .h>
  2. #include <string.h>
  3.  
  4. char * myStrrstr(const char *haystack, const char *needle);
  5.  
  6. int
  7. main(void)
  8. {
  9.     char *s = "hello world";
  10.     char *t = "ll";
  11.     char *r = myStrrstr(s, t);
  12.     printf(r);
  13.  
  14.     return 0;
  15. }
  16.  
  17. char * myStrrstr(const char *haystack, const char *needle)
  18. {
  19.     unsigned int i;
  20.      unsigned int hay_len, need_len;
  21.     const char *p;
  22.  
  23.     if (NULL == haystack || NULL == needle)
  24.         return NULL;
  25.  
  26.  
  27.     hay_len = strlen(haystack);
  28.     need_len = strlen(needle);
  29.  
  30.     if (need_len == 0)
  31.         return (char *)haystack;
  32.  
  33.     if (hay_len < need_len)
  34.         return NULL;
  35.  
  36.     p = haystack + hay_len - need_len;
  37.  
  38.     while (p >= haystack)
  39.  {
  40.         for (i = 0; i < need_len; i++)
  41.             if (p[i] != needle[i])
  42.                 goto next;
  43.         return (char *)p;
  44.         
  45.         next:
  46.             p--;
  47.   }
  48.  
  49.     return NULL;
  50. }
 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
ADI 高精度低功耗精密放大器