W C/C++ std::strstr() jest predefiniowaną funkcją używaną do dopasowywania ciągów. to plik nagłówkowy wymagany dla funkcji łańcuchowych. Ta funkcja pobiera dwa ciągi znaków s1 I s2 jako argumenty i znajduje pierwsze wystąpienie ciągu s2 w sznurku s1 . Proces dopasowywania nie obejmuje kończących znaków zerowych („ ”), ale funkcja na tym się kończy.
Składnia
char * strstr (const char * s1 , const char * s2 );>
Parametry
- s1 : To jest główny ciąg do sprawdzenia. s2: To jest podciąg, który ma być przeszukiwany w ciągu.
Wartość zwracana
- Ta funkcja zwraca punkt wskaźnika do pierwszego znalezionego znaku s2 W s1 w przeciwnym razie wskaźnik zerowy if s2 nie występuje w s1 .
- Jeżeli s2 wskazuje na pusty ciąg znaków, zwracane jest s1.
Przykład
Poniższy program ilustruje użycie funkcji 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;> }> |
>
>Wyjście
String found First occurrence of string 'for' in 'techcodeview.com' is 'forGeeks'>
Złożoność czasowa: O(n + m), gdzie n jest rozmiarem s1, a m jest rozmiarem s2.
Przestrzeń pomocnicza: O(m), gdzie m jest wielkością s2.
Notatka: Oficjalna implementacja strstr() nie jest określona; zakłada się, że jej implementacja składa się z dowolnego ze standardowych algorytmów dopasowywania ciągów. Tutaj założyliśmy, że jest on realizowany przy użyciu algorytmu Knutha-Morrisa-Pratta, który ma złożoność czasową i przestrzenną, jak podano powyżej.
Aplikacja : Zamień ciąg na inny
W tym przykładzie za pomocą funkcji strstr() najpierw szukamy wystąpienia podłańcucha STL W s1 a następnie zamień to słowo na Smyczki .
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;> }> |
>
... w Javie
>Wyjście
Fun with Strings>
Złożoność czasowa: O(n + m), gdzie n jest rozmiarem s1, a m jest rozmiarem s2.
Przestrzeń pomocnicza: O(m), gdzie m jest wielkością s2.