Analysis and Implementation of the C language function strstr ()

Source: Internet
Author: User

The following is a detailed analysis of the use of the strstr () function in C language. For more information, see

Prototype: char * strstr (const char * str1, const char * str2 );
# Include <string. h>
Locate the position where the str2 string first appears in the str1 string (excluding the str2 string Terminator ). Returns the pointer to this position. If no pointer is found, a null pointer is returned.
Returns a pointer to the first occurrence of strSearch in str, or NULL if strSearch does not appear in str. IfstrSearch points to a string of zero length, the function returns str.

Copy codeThe Code is as follows:
# Include <stdio. h>
# Include <conio. h>
# Include <string. h>
# Include <stdlib. h>
# 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 considerations
{
While (* s1)
{
For (n = 0; * (s1 + n) = * (s2 + n); n ++)
{
If (! * (S2 + n + 1) // determines whether the next character to be searched is''
{
Return (char *) s1;
}
}
S1 ++;
}
Return NULL;
}
Else
{
Return (char *) s1;
}
}


Another implementation:

Copy codeThe Code is 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 codeThe Code is as follows:


# Include <iostream>
# Include <string>
Using namespace std;
// Implement strstr in C Language
Const char * isSub (const char * str, const char * subs ){
// Special Case
If (! * Subs)
Return str;
Const char * tmp = str;
While (* tmp! = '')
{
// Used to move the parent string one character backward each time
Const char * tmp1 = tmp;
// Record the substring address
Const char * sub1 = subs;
While (* sub1! = ''& * Tmp1! = '')
{
// Jumps out if they are not equal, and removes a character from the parent string
If (* sub1! = * Tmp1)
Break;
// If the child string is equal and the next character is the end of the Child string, it is the child string of the parent string.
If (* sub1 = * tmp1 & * (sub1 + 1) = '')
Return tmp;
// If the values are equal, continue to compare the next character.
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
Cout <"null" <endl;
// Cout <isSub (str1, str) <endl;
Return 0;
}

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.