V C/C++ je std::strstr() vnaprej določena funkcija, ki se uporablja za ujemanje nizov. je datoteka glave, potrebna za funkcije nizov. Ta funkcija sprejme dva niza s1 in s2 kot argumente in najde prvo pojavitev niza s2 v nizu s1 . Postopek ujemanja ne vključuje zaključnih ničelnih znakov (' '), vendar se funkcija tam ustavi.
Sintaksa
char * strstr (const char * s1 , const char * s2 );>
Parametri
- s1 : To je glavni niz, ki ga je treba pregledati. s2 : To je podniz, ki ga je treba iskati v nizu.
Povratna vrednost
- Ta funkcija vrne točko kazalca na prvi najdeni znak s2 v s1 sicer ničelni kazalec if s2 ni prisoten v s1 .
- Če s2 kaže na prazen niz, se vrne s1.
Primer
Spodnji program ponazarja uporabo funkcije 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;> }> |
>
>Izhod
String found First occurrence of string 'for' in 'techcodeview.com' is 'forGeeks'>
Časovna zapletenost: O(n + m), kjer je n velikost s1 in m velikost s2.
Pomožni prostor: O(m), kjer je m velikost s2.
Opomba: Uradna izvedba strstr() ni določena, predpostavlja se, da je sestavljena iz katerega koli standardnega algoritma za ujemanje nizov. Tukaj smo predpostavili, da je implementiran z algoritmom Knuth-Morris-Pratt, ki ima časovno in prostorsko kompleksnost, kot je navedeno zgoraj.
Aplikacija : Zamenjaj niz z drugim
V tem primeru s pomočjo funkcije strstr() najprej iščemo pojavitev podniza STL v s1 in po tem zamenjajte to besedo z Strune .
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;> }> |
>
kaj je poseben znak
>Izhod
Fun with Strings>
Časovna zapletenost: O(n + m), kjer je n velikost s1 in m velikost s2.
Pomožni prostor: O(m), kjer je m velikost s2.