Analysis and implementation of C-language function strstr ()

Source: Internet
Author: User
Tags printf string back
The following is a detailed analysis of the use of strstr () function in C language, the need for friends can refer to the next

Prototype: Char *strstr (const char *STR1, const char *STR2);
#include <string.h>
finds the first occurrence of the STR2 string in the STR1 string (excluding the STR2 string terminator). Returns a pointer to the position, such as if it is not found, and returns a null pointer.
Returns a pointer to the ' the ' of strsearch in str, or NULL if Strsearch does is not appear in Str. ifstrsearch PO INTs to a string of zero length, the function returns STR.

Copy Code code as follows:


#include &lt;stdio.h&gt;


#include &lt;conio.h&gt;


#include &lt;string.h&gt;


#include &lt;stdlib.h&gt;


#pragma warning (disable:4996)


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


int main (void)


{


Char *s= "Golden Global View";


char *l= "OB"; Char *l= ""


Char *p;


System ("CLS");


P=mystrstr (s,l);


if (p!=null)


 {


printf ("%sn", p);


 }


Else


 {


printf ("Not found!n");


 }


Getch ();


return 0;


}


/*from Encyclopedia * *


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


{


int n;


if (*S2)//Two situation considerations


 {


while (*S1)


  {


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


{


if (!* (s2+n+1))//lookup next character is '
'

    {


return (char*) S1;


    }


            }


s1++;


  }


return NULL;


 }


Else


 {


return (char*) S1;


 }


}


Another implementation:

Copy Code code as follows:


char * STRSTR (BUF, sub)


Register char *buf;


Register char *sub;


{


Register char *BP;


Register char *sp;


if (!*sub)


return buf;


while (*BUF)


    {


bp = buf;


sp = sub;


do {


if (!*SP)


return buf;


} while (*bp++ = = *sp++);


buf + + 1;


    }


return 0;


}


Another implementation:

Copy Code The code is as follows:


#include <iostream>
#include <string>
using namespace std;
//c Language Implementation strstr
Const char* Issub (const char* STR, const char *subs) {
 //special case
 if (!*subs)
  return str;
  Const char* TMP=STR;
 while (*tmp!= ')
 {
  //is used to move the parent string back one character at a time
  const char* tmp1=tmp;
& nbsp; //The record substring address
  const char* sub1=subs;
  while (*sub1!= ' &&*tmp1!= ')
  {
   //If not equal jumps out, moves the parent string one character back
   if (*SUB1!=*TMP1)
     break;
   //If equal and the next character at the end of the substring is the substring of the parent string
   if (*sub1==*tmp1&&* (sub1+1) = = ")
    return tmp;
   //continues to compare the next character if equal
   if (*SUB1==*TMP1)
   {
     sub1++;
    tmp1++;
   }
  }
  tmp++;
 }
 return NULL;
}
int main () {
 char* str1= "Ababcdddb";
 char* str= "";
 const Char *res=issub (STR1, STR);
 if (res!=null)
 {
  cout << res << endl;
 }
 else
&nb Sp; cout << "null" << Endl;
 //cout << issub (str1,str) << Endl;
 return 0;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.