V C/C++ je std::strstr() předdefinovaná funkce používaná pro porovnávání řetězců. je soubor záhlaví požadovaný pro funkce řetězce. Tato funkce vyžaduje dva řetězce s1 a s2 jako argumenty a najde první výskyt řetězce s2 v řetězci s1 . Proces porovnávání nezahrnuje ukončující znaky null(‘ ’), ale funkce se tam zastaví.
Syntax
char * strstr (const char * s1 , const char * s2 );>
Parametry
- s1 : Toto je hlavní řetězec, který má být zkoumán. s2 : Toto je podřetězec, který se má hledat v řetězci.
Návratová hodnota
- Tato funkce vrací ukazatel na první znak nalezeného s2 v s1 jinak nulový ukazatel if s2 není přítomen v s1 .
- Pokud s2 ukazuje na prázdný řetězec, je vráceno s1.
Příklad
Níže uvedený program ilustruje použití funkce 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ýstup
String found First occurrence of string 'for' in 'techcodeview.com' is 'forGeeks'>
Časová náročnost: O(n + m), kde n je velikost s1 a m je velikost s2.
Pomocný prostor: O(m), kde m je velikost s2.
Poznámka: Oficiální implementace strstr() není specifikována, předpokládá se, že její implementace sestává z libovolného ze standardních algoritmů pro porovnávání řetězců. Zde jsme předpokládali, že je implementován pomocí Knuth-Morris-Prattova algoritmu, který má časovou a prostorovou složitost, jak je uvedeno výše.
aplikace : Nahraďte řetězec jiným
V tomto příkladu pomocí funkce strstr() nejprve hledáme výskyt podřetězce STL v s1 a poté 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;> }> |
>
co je mac os
>Výstup
Fun with Strings>
Časová náročnost: O(n + m), kde n je velikost s1 a m je velikost s2.
Pomocný prostor: O(m), kde m je velikost s2.