库函数strstr的实现

函数strstr的原型是char *strstr(char *str1, char *str2); 其功能是在str1中返回指定字符串str2的第一次出现的位置。 

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. int main(void)  
  4. {  
  5.     char *str1 = "Borland International", *str2 = "nation", *ptr;  
  6.     ptr = strstr(str1, str2);  
  7.     printf("The substring is: %s/n", ptr);  
  8.     return 0;  
  9. }   
  10. /* 
  11. output: 
  12. The substring is: national  
  13. */  

下面是实现strstr功能的一种方法。(注:有错误! )

  1. /* 
  2.    Modification date: 2010-5-28 
  3.    test: 
  4.    src: abcd efghcd abcdef 
  5.    dst: cd 
  6.    return: cd efghcd abcdef 
  7. */  
  8. #include <cstdio>   
  9. #include <cstring>// strlen, strstr  
  10. #include <cassert>// assert  
  11. char* my_strstr(char* src,char* dst)  
  12. {     
  13.     // check  
  14.     assert(src);  
  15.     assert(dst);  
  16.       
  17.     // calculate the each size    
  18.     char *szSrc = src,*szDst = dst;  
  19.     int nSrcSize = strlen(szSrc), nDstSize = strlen(szDst);  
  20.     // check  
  21.     if (nSrcSize < nDstSize)  
  22.     {  
  23.         return NULL;// error  
  24.     }  
  25.     // compare  
  26.     int nCompareCount = nSrcSize-nDstSize+1;  
  27.     int i,j;  
  28.     for (i = j = 0; i<nSrcSize && j<nDstSize; ++i)  
  29.     {  
  30.         if (src[i] != dst[j])  
  31.         {  
  32.             if (i == nCompareCount-1)// this check can save time  
  33.             {  
  34.                 return NULL;// no substring  
  35.             }  
  36.             j = 0;  
  37.         }  
  38.         else// to find the first same value  
  39.         {  
  40.             ++j;  
  41.         }  
  42.     }  
  43.     if (j == nDstSize)// absolute equal  
  44.     {                     
  45.         return &src[i-nDstSize];// have substring  
  46.     }  
  47.     return NULL;// no substring  
  48. }  
  49. int main()  
  50. {   
  51.     //char *szSrc = "wcdj is a guy", *szDst = "is";  
  52.     char szSrc[128] = {0}, szDst[128] = {0};  
  53.     printf("input src: ");  
  54.     //scanf("%s",szSrc);  
  55.     gets(szSrc);  
  56.     printf("input dst: ");  
  57.     //scanf("%s",szDst);  
  58.     gets(szDst);  
  59.     // call my function  
  60.     char* ptr = my_strstr(szSrc,szDst);  
  61.     if (ptr)  
  62.     {  
  63.         printf("The substring is: %s/n", ptr);  
  64.     }   
  65.     else  
  66.     {  
  67.         printf("has no substring/n");  
  68.     }  
  69.     getchar();  
  70.     return 0;   
  71. }  

 

上述方法只考虑了目的字符串dst的回溯,但是没有考虑源字符串src的回溯,纠正如下:使用nNext记录回溯的位置。

  1. /* 
  2.    Modification date: 2011-4-13 
  3.    test: 
  4.    src: xxy 
  5.    dst: xy 
  6.    return: xy 
  7. */  
  8. #include <cstdio>   
  9. #include <cstring>// strlen, strstr  
  10. #include <cassert>// assert  
  11. char* my_strstr(char* src, char* dst)  
  12. {     
  13.     // check  
  14.     assert(src);  
  15.     assert(dst);  
  16.       
  17.     // calculate the each size    
  18.     char *szSrc = src,*szDst = dst;  
  19.     int nSrcSize = strlen(szSrc), nDstSize = strlen(szDst);  
  20.     // check  
  21.     if (nSrcSize < nDstSize)  
  22.     {  
  23.         return NULL;// error  
  24.     }  
  25.     // compare    
  26.     int nCompareCount = nSrcSize-nDstSize+1;   
  27.     int nNext = 0;  
  28.     bool bFirst = false;  
  29.     int i = 0, j = 0;  
  30.     while (i < nSrcSize && j < nDstSize)  
  31.     {  
  32.         if (src[i] != dst[j])  
  33.         {  
  34.             if (--nCompareCount == 0)// save time    
  35.             {    
  36.                 return NULL;// no substring    
  37.             }    
  38.             // backtrace  
  39.             j = 0;  
  40.             if (bFirst)  
  41.             {  
  42.                 i = nNext;  
  43.                 bFirst = false;  
  44.                 continue;  
  45.             }             
  46.         }  
  47.         else  
  48.         {  
  49.             ++j;  
  50.             if (bFirst == false)  
  51.             {  
  52.                 nNext = i+1;  
  53.                 bFirst = true;  
  54.             }  
  55.         }  
  56.         ++i;  
  57.     }  
  58.     if (j == nDstSize)// absolute equal  
  59.     {                     
  60.         return &src[i-nDstSize];// have substring  
  61.     }  
  62.     return NULL;// no substring  
  63. }  
  64. int main()  
  65. {   
  66.     char szSrc[128] = {0}, szDst[128] = {0};  
  67.     printf("input src: ");  
  68.     fgets(szSrc, sizeof(szSrc), stdin);  
  69.     printf("input dst: ");  
  70.     fgets(szDst, sizeof(szDst), stdin);  
  71.     // clear '/n' in the end of string  
  72.     szSrc[strlen(szSrc)-1] = '/0';  
  73.     szDst[strlen(szDst)-1] = '/0';  
  74.     char* ptr = my_strstr(szSrc,szDst);  
  75.     if (ptr)  
  76.     {  
  77.         printf("The substring is: %s/n", ptr);  
  78.     }   
  79.     else  
  80.     {  
  81.         printf("has no substring/n");  
  82.     }  
  83.     getchar();  
  84.     return 0;   
  85. }  

 


学习:http://blog.csdn.net/delphiwcdj/article/details/4620793

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值