V C/C++ je std::strstr() preddefinovaná funkcia používaná na porovnávanie reťazcov. je hlavičkový súbor potrebný pre funkcie reťazca. Táto funkcia má dva reťazce s1 a s2 ako argumenty a nájde prvý výskyt reťazca s2 v reťazci s1 . Proces porovnávania nezahŕňa ukončovacie null-znaky (' '), ale funkcia sa tam zastaví.
Syntax
char * strstr (const char * s1 , const char * s2 );>
Parametre
- s1 : Toto je hlavný reťazec, ktorý sa má preskúmať. s2 : Toto je podreťazec, ktorý sa má hľadať v reťazci.
Návratová hodnota
- Táto funkcia vráti bod ukazovateľa na prvý znak nájdeného znaku s2 v s1 inak nulový ukazovateľ if s2 nie je prítomný v s1 .
- Ak s2 ukazuje na prázdny reťazec, vráti sa s1.
Príklad
Nižšie uvedený program ilustruje použitie funkcie 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;> }> |
>
>Výkon
String found First occurrence of string 'for' in 'techcodeview.com' is 'forGeeks'>
Časová zložitosť: O(n + m), kde n je veľkosť s1 a m je veľkosť s2.
Pomocný priestor: O(m), kde m je veľkosť s2.
Poznámka: Oficiálna implementácia strstr() nie je špecifikovaná, predpokladá sa, že jej implementácia pozostáva z ktoréhokoľvek zo štandardných algoritmov na porovnávanie reťazcov. Tu sme predpokladali, že je implementovaný pomocou algoritmu Knuth-Morris-Pratt, ktorý má časovú a priestorovú zložitosť, ako je uvedené vyššie.
Aplikácia : Nahraďte reťazec iným
V tomto príklade pomocou funkcie strstr() najskôr hľadáme výskyt podreťazca STL v s1 a potom toto slovo nahraďte Struny .
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;> }> |
>
10 1 milióna
>Výkon
Fun with Strings>
Časová zložitosť: O(n + m), kde n je veľkosť s1 a m je veľkosť s2.
Pomocný priestor: O(m), kde m je veľkosť s2.