On the analysis of C language function strstr () and the realization of _c language

Source: Internet
Author: User
Tags string back
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 <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 ("%s\n", p);
}
Else
{
printf ("Not found!\n");
}
Getch ();
return 0;
}
/*from Encyclopedia * *
Char *mystrstr (char *s1,char *s2)
{
int n;
if (*S2)//Two case considerations
{
while (*S1)
{
For (n=0;* (s1+n) ==* (s2+n); n++)
{
if (!* (s2+n+1))///Find the next character is ' I '
{
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 code as follows:

#include <iostream>
#include <string>
using namespace Std;
C Language Implementation Strstr
Const char* issub (const char* STR, const char *subs) {
Special cases
if (!*subs)
return str;
Const char* TMP=STR;
while (*tmp!= ' ")
{
Used to move the parent string back one character at a time
Const char* TMP1=TMP;
Record substring address
Const char* Sub1=subs;
while (*sub1!= ' &&*tmp1!= ')
{
If not equal, jump out, move the parent string back one character
if (*SUB1!=*TMP1)
Break
If equal and the next character of the substring is the end, then the substring of the parent string
if (*sub1==*tmp1&&* (sub1+1) = = ' * ')
return TMP;
If 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.