In C/C++ is std::strstr() een vooraf gedefinieerde functie die wordt gebruikt voor het matchen van tekenreeksen. is het headerbestand dat vereist is voor stringfuncties. Deze functie heeft twee strings nodig s1 En s2 als argumenten en zoekt naar de eerste keer dat de tekenreeks voorkomt s2 in het touwtje s1 . Het matchingproces omvat niet de afsluitende nultekens (‘ ’), maar de functie stopt daar.
Syntaxis
char * strstr (const char * s1 , const char * s2 );>
Parameters
- s1: Dit is de hoofdreeks die moet worden onderzocht. s2 : Dit is de subtekenreeks waarnaar in de tekenreeks moet worden gezocht.
Winstwaarde
- Deze functie retourneert een verwijzingspunt naar het eerste teken van de gevonden tekst s2 in s1 anders een nulaanwijzer als s2 is niet aanwezig s1 .
- Als s2 naar een lege string verwijst, wordt s1 geretourneerd.
Voorbeeld
Het onderstaande programma illustreert het gebruik van de functie strstr().
C
// C program to illustrate strstr()> #include> #include> int> main()> {> >// Take any two strings> >char> s1[] =>'techcodeview.com'>;> >char> s2[] =>'for'>;> >char>* p;> >// Find first occurrence of s2 in s1> >p =>strstr>(s1, s2);> >// Prints the result> >if> (p) {> >printf>(>'String found
'>);> >printf>(>'First occurrence of string '%s' in '%s' is '> >''%s''>,> >s2, s1, p);> >}> >else> >printf>(>'String not found
'>);> >return> 0;> }> |
>
>
C++
// CPP program to illustrate strstr()> #include> #include> using> namespace> std;> int> main()> {> >// Take any two strings> >char> s1[] =>'techcodeview.com'>;> >char> s2[] =>'for'>;> >char>* p;> >// Find first occurrence of s2 in s1> >p =>strstr>(s1, s2);> >// Prints the result> >if> (p) {> >cout <<>'String found'> << endl;> >cout <<>'First occurrence of string ''> << s2> ><<>'' in ''> << s1 <<>'' is ''> << p <<>'''> ><< endl;> >}> >else> {> >cout <<>'String not found'> << endl;> >}> >return> 0;> }> |
>
>Uitvoer
String found First occurrence of string 'for' in 'techcodeview.com' is 'forGeeks'>
Tijdcomplexiteit: O(n + m), waarbij n de grootte is van s1 en m de grootte is van s2.
Hulpruimte: O(m), waarbij m de grootte is van s2.
Opmerking: De officiële implementatie van strstr() is niet gespecificeerd; er wordt aangenomen dat de implementatie ervan bestaat uit een van de standaard algoritmen voor het matchen van tekenreeksen. Hier hebben we aangenomen dat het wordt geïmplementeerd met behulp van het Knuth-Morris-Pratt-algoritme, dat tijd- en ruimtecomplexiteit heeft, zoals hierboven vermeld.
Sollicitatie : Vervang een string door een andere
In dit voorbeeld zoeken we met behulp van de functie strstr() eerst naar het voorkomen van substring STL in s1 en vervang daarna dat woord door Snaren .
C++
// CPP program to illustrate strstr()> #include> #include> using> namespace> std;> int> main()> {> >// Take any two strings> >char> s1[] =>'Fun with STL'>;> >char> s2[] =>'STL'>;> >char>* p;> >// Find first occurrence of s2 in s1> >p =>strstr>(s1, s2);> >// Prints the result> >if> (p) {> >strcpy>(p,>'Strings'>);> >cout << s1;> >}> >else> {> >cout <<>'String not found'> << endl;> >}> >return> 0;> }> |
>
>
C
// C program to illustrate strstr()> #include> #include> int> main()> {> >// Take any two strings> >char> s1[] =>'Fun with STL'>;> >char> s2[] =>'STL'>;> >char>* p;> >// Find first occurrence of s2 in s1> >p =>strstr>(s1, s2);> >// Prints the result> >if> (p) {> >strcpy>(p,>'Strings'>);> >printf>(>'%s'>, s1);> >}> >else> >printf>(>'String not found
'>);> >return> 0;> }> |
>
volledig optelcircuit
>Uitvoer
Fun with Strings>
Tijdcomplexiteit: O(n + m), waarbij n de grootte is van s1 en m de grootte is van s2.
Hulpruimte: O(m), waarbij m de grootte is van s2.