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