函数strstr的原型是char *strstr(char *str1, char *str2); 其功能是在str1中返回指定字符串str2的第一次出现的位置。
- #include <stdio.h>
- #include <string.h>
- int main(void)
- {
- char *str1 = "Borland International", *str2 = "nation", *ptr;
- ptr = strstr(str1, str2);
- printf("The substring is: %s/n", ptr);
- return 0;
- }
- /*
- output:
- The substring is: national
- */
下面是实现strstr功能的一种方法。(注:有错误! )
- /*
- Modification date: 2010-5-28
- test:
- src: abcd efghcd abcdef
- dst: cd
- return: cd efghcd abcdef
- */
- #include <cstdio>
- #include <cstring>// strlen, strstr
- #include <cassert>// assert
- char* my_strstr(char* src,char* dst)
- {
- // check
- assert(src);
- assert(dst);
- // calculate the each size
- char *szSrc = src,*szDst = dst;
- int nSrcSize = strlen(szSrc), nDstSize = strlen(szDst);
- // check
- if (nSrcSize < nDstSize)
- {
- return NULL;// error
- }
- // compare
- int nCompareCount = nSrcSize-nDstSize+1;
- int i,j;
- for (i = j = 0; i<nSrcSize && j<nDstSize; ++i)
- {
- if (src[i] != dst[j])
- {
- if (i == nCompareCount-1)// this check can save time
- {
- return NULL;// no substring
- }
- j = 0;
- }
- else// to find the first same value
- {
- ++j;
- }
- }
- if (j == nDstSize)// absolute equal
- {
- return &src[i-nDstSize];// have substring
- }
- return NULL;// no substring
- }
- int main()
- {
- //char *szSrc = "wcdj is a guy", *szDst = "is";
- char szSrc[128] = {0}, szDst[128] = {0};
- printf("input src: ");
- //scanf("%s",szSrc);
- gets(szSrc);
- printf("input dst: ");
- //scanf("%s",szDst);
- gets(szDst);
- // call my function
- char* ptr = my_strstr(szSrc,szDst);
- if (ptr)
- {
- printf("The substring is: %s/n", ptr);
- }
- else
- {
- printf("has no substring/n");
- }
- getchar();
- return 0;
- }
上述方法只考虑了目的字符串dst的回溯,但是没有考虑源字符串src的回溯,纠正如下:使用nNext记录回溯的位置。
- /*
- Modification date: 2011-4-13
- test:
- src: xxy
- dst: xy
- return: xy
- */
- #include <cstdio>
- #include <cstring>// strlen, strstr
- #include <cassert>// assert
- char* my_strstr(char* src, char* dst)
- {
- // check
- assert(src);
- assert(dst);
- // calculate the each size
- char *szSrc = src,*szDst = dst;
- int nSrcSize = strlen(szSrc), nDstSize = strlen(szDst);
- // check
- if (nSrcSize < nDstSize)
- {
- return NULL;// error
- }
- // compare
- int nCompareCount = nSrcSize-nDstSize+1;
- int nNext = 0;
- bool bFirst = false;
- int i = 0, j = 0;
- while (i < nSrcSize && j < nDstSize)
- {
- if (src[i] != dst[j])
- {
- if (--nCompareCount == 0)// save time
- {
- return NULL;// no substring
- }
- // backtrace
- j = 0;
- if (bFirst)
- {
- i = nNext;
- bFirst = false;
- continue;
- }
- }
- else
- {
- ++j;
- if (bFirst == false)
- {
- nNext = i+1;
- bFirst = true;
- }
- }
- ++i;
- }
- if (j == nDstSize)// absolute equal
- {
- return &src[i-nDstSize];// have substring
- }
- return NULL;// no substring
- }
- int main()
- {
- char szSrc[128] = {0}, szDst[128] = {0};
- printf("input src: ");
- fgets(szSrc, sizeof(szSrc), stdin);
- printf("input dst: ");
- fgets(szDst, sizeof(szDst), stdin);
- // clear '/n' in the end of string
- szSrc[strlen(szSrc)-1] = '/0';
- szDst[strlen(szDst)-1] = '/0';
- char* ptr = my_strstr(szSrc,szDst);
- if (ptr)
- {
- printf("The substring is: %s/n", ptr);
- }
- else
- {
- printf("has no substring/n");
- }
- getchar();
- return 0;
- }