<delect id="sj01t"></delect>
  1. <em id="sj01t"><label id="sj01t"></label></em>
  2. <div id="sj01t"></div>
    1. <em id="sj01t"></em>

            <div id="sj01t"></div>
            C語言

            C語言入門知識:strstr函數

            時間:2025-03-28 14:14:48 C語言 我要投稿
            • 相關推薦

            C語言入門知識:strstr函數

              導語:strstr(str1,str2) 函數用于判斷字符串str2是否是str1的子串。如果是,則該函數返回str2在str1中首次出現的地址;否則,返回NULL。下面是C語言strstr函數知識,歡迎閱讀:

            C語言入門知識:strstr函數

              C語言函數

              包含文件:string.h

              函數名: strstr

              函數原型:

              1

              extern char *strstr(char *str1, const char *str2);

              語法:

              1

              * strstr(str1,str2)

              str1: 被查找目標 string expression to search.

              str2: 要查找對象 The string expression to find.

              返回值:若str2是str1的子串,則返回str2在str1的首次出現的地址;如果str2不是str1的子串,則返回NULL。

              例子:

              1

              2

              3

              char str[]="1234xyz";

              char *str1=strstr(str,"34");

              cout << str1 << endl;

              顯示的是: 34xyz

              函數實現

              1.Copyright 1990 Software Development Systems, Inc.

              1

              2

              3

              4

              5

              6

              7

              8

              9

              10

              11

              12

              char *strstr(const char *s1,const char *s2)

              {

              int len2;

              if(!(len2=strlen(s2)))//此種情況下s2不能指向空,否則strlen無法測出長度,這條語句錯誤

              return(char*)s1;

              for(;*s1;++s1)

              {

              if(*s1==*s2 && strncmp(s1,s2,len2)==0)

              return(char*)s1;

              }

              return NULL;

              }

              2.Copyright 1986 - 1999 IAR Systems. All rights reserved

              1

              2

              3

              4

              5

              6

              7

              8

              9

              10

              11

              12

              13

              14

              15

              16

              17

              18

              19

              char *strstr(constchar*s1,constchar*s2)

              {

              int n;

              if(*s2)

              {

              while(*s1)

              {

              for(n=0;*(s1+n)==*(s2+n);n++)

              {

              if(!*(s2+n+1))

              return(char*)s1;

              }

              s1++;

              }

              return NULL;

              }

              else

              return (char*)s1;

              }

              3. GCC-4.8.0

              1

              2

              3

              4

              5

              6

              7

              8

              9

              10

              11

              char *strstr(const char*s1,const char*s2)

              {

              const char*p=s1;

              const size_tlen=strlen(s2);

              for(;(p=strchr(p,*s2))!=0;p++)

              {

              if(strncmp(p,s2,len)==0)

              return (char*)p;

              }

              return(0);

              }

              應用舉例

              // strstr.c

              1

              2

              3

              4

              5

              6

              7

              8

              9

              10

              11

              12

              13

              14

              15

              16

              #include

              #include

              main()

              {

              char *s="GoldenGlobalView";

              char *l="lob";

              char *p;

              clrscr();

              p=strstr(s,l);

              if(p)

              printf("%s",p);

              else

              printf("NotFound!");

              get);

              return0;

              }

              //功能:從字串” string1 onexxx string2 oneyyy”中尋找”yyy”

              (假設xxx和yyy都是一個未知的字串)

              1

              2

              3

              4

              5

              6

              7

              char *s=”string1onexxxstring2oneyyy”;

              char *p;

              p=strstr(s,”yyy”);

              if(p!=NULL)

              printf(“%s”,p);

              else

              printf("notfound ");

              說明:如果直接寫語句p=strstr(s,”one”),找到的是onexxxstring2oneyyy

              1

              2

              3

              4

              5

              6

              7

              8

              9

              10

              11

              12

              13

              14

              15

              16

              17

              18

              19

              20

              21

              22

              23

              char *mystrstr(char*s1,char*s2)

              {

              if(*s1==0)

              {

              if(*s2)

              return (char*)NULL;

              return (char*)s1;

              }

              while(*s1)

              {

              int i=0;

              while(1)

              {

              if(s2[i]==0)

              return s1;

              if(s2[i]!=s1[i])

              break;

              i++;

              }

              s1++;

              }

              return (char*)NULL;

              }


            【C語言入門知識:strstr函數】相關文章:

            C語言入門知識:strchr函數07-24

            C語言入門知識:realloc函數07-05

            C語言中strstr()函數的使用分析08-03

            C語言函數入門學習06-02

            C語言入門知識07-20

            C語言入門必備知識10-03

            C語言入門知識:常量08-31

            C語言中gets()函數知識08-10

            c語言入門基礎知識07-18

            <delect id="sj01t"></delect>
            1. <em id="sj01t"><label id="sj01t"></label></em>
            2. <div id="sj01t"></div>
              1. <em id="sj01t"></em>

                      <div id="sj01t"></div>
                      黄色视频在线观看