logo

strstr() in C/C++

In C/C++, std::strstr() è una funzione predefinita utilizzata per la corrispondenza delle stringhe. è il file di intestazione richiesto per le funzioni di stringa. Questa funzione richiede due stringhe s1 E s2 come argomenti e trova la prima occorrenza della stringa s2 nella corda s1 . Il processo di corrispondenza non include i caratteri nulli terminanti (' '), ma la funzione si ferma qui.

Sintassi

char * strstr  (const char * s1 , const char * s2 );>

Parametri

    s1: questa è la stringa principale da esaminare. s2: questa è la sottostringa da cercare nella stringa.

Valore di ritorno

  • Questa funzione restituisce un puntatore al primo carattere del trovato s2 In s1 altrimenti un puntatore nullo se s2 non è presente in s1 .
  • Se s2 punta a una stringa vuota, viene restituito s1.

Esempio

Il programma seguente illustra l'utilizzo della funzione 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;> }>

>

>

Produzione

String found First occurrence of string 'for' in 'techcodeview.com' is 'forGeeks'>

Complessità temporale: O(n + m), dove n è la dimensione di s1 e m è la dimensione di s2.
Spazio ausiliario: O(m), dove m è la dimensione di s2.

Nota: L'implementazione ufficiale di strstr() non è specificata, si presuppone che consista in uno qualsiasi degli algoritmi standard di corrispondenza delle stringhe. Qui, abbiamo ipotizzato che sia implementato utilizzando l'algoritmo di Knuth-Morris-Pratt, che ha complessità temporale e spaziale come indicato sopra.

Applicazione : Sostituisci una stringa con un'altra

In questo esempio con l'aiuto della funzione strstr() cerchiamo prima l'occorrenza della sottostringa STL In s1 e dopo sostituisci quella parola con stringhe .

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;> }>

>

scaricare video da youtube con vlc
>

Produzione

Fun with Strings>

Complessità temporale: O(n + m), dove n è la dimensione di s1 e m è la dimensione di s2.
Spazio ausiliario: O(m), dove m è la dimensione di s2.