庫函式strstr的實現

2021-05-24 11:01:00 字數 1819 閱讀 1404

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

view plaincopy to clipboardprint?

01.#include

02.#include

03.int main(void)  

04.   

10./* 

11.output: 

12.the substring is: national  

13.*/ 

#include

#include

int main(void) /*

output:

the substring is: national

*/ 下面是實現strstr功能的一種方法。

view plaincopy to clipboardprint?

01./* 

02.modification date: 2010-5-28 

03.src: abcd efghcd abcdef 

04.dst:cd 

05.return: cd efghcd abcdef 

06.*/ 

07.#include

08.#include // strlen  

09.#include // assert  

10.#include // getch  

11.//#include // strstr  

12.#define null 0;  

13.char* strstr(char* src,char* dst)  

14.  

28.    // compare  

29.    int ncomparecount=nsrcsize-ndstsize+1;  

30.    int i,j;  

31.    for (i=j=0; i32.      

39.            j=0;  

40.        }  

41.        else// to find the first same value  

42.          

45.    }  

46.    if (j==ndstsize)// absolute equal  

47.      

50.    return null;// no substring  

51.}  

52.int main()  

53.,szdst[128]=;  

56.    printf("input src: ");  

57.    //scanf("%s",szsrc);  

58.    gets(szsrc);  

59.    printf("input dst: ");  

60.    //scanf("%s",szdst);  

61.    gets(szdst);  

62.    // call my function  

63.    char* ptr=strstr(szsrc,szdst);  

64.    if (ptr)  

65.       

68.    else 

69.      

72.    getch();  

73.    return 0;   

74.} 

庫函式strstr的實現

沒什麼說的,常規思路 函式原型 const char strstr const char str1,const char str2 方法一 str1 源字串 str2 需要查詢的目的字串 pragma once includeconst char my strstr const char str1,...

標準庫函式實現之strstr

昨天去參加乙個面試,發現自己的 水平還是不夠,謝了兩次才寫出來,連基本的標準庫實現,都沒法做好,遂決定對標準庫中的部分函式的實現研習一番。面試的是 char strstr char s1 char s2 函式,自己寫的就不說了,寫得很爛,不過看了minix 裡面的實現思路和我一樣,linux的 中,...

實現庫函式strstr和strchr

下面是實現庫函式時用到的標頭檔案 define crt secure no warnings 1 include include include strstr用於判斷字串str2是否是str1的子串。如果是,則該函式返回str1字串從str2第一次出現的位置開始到結尾的字串 否則,返回null。ch...