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 <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 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;
}