logo

stringa trova in C++

Corda find viene utilizzato per trovare la prima occorrenza di una sottostringa nella stringa specificata richiamata. Restituisce l'indice della prima occorrenza della sottostringa nella stringa dalla posizione iniziale specificata. Il valore predefinito della posizione iniziale è 0.

inizializzazione dell'elenco Python

È una funzione membro di std::stringa classe.



Sintassi:

size_t find (const string& str, size_t pos = 0); // for C++ Style Strings or size_t find (const char* s, size_t pos = 0); // for C-Style Strings>

parametri:

    str: la sottostringa da cercare. s: la sottostringa da cercare, fornita come stringa in stile C. pos: la posizione iniziale da cui deve iniziare la ricerca della stringa.

Valore di ritorno:



  • La funzione restituisce l'indice della prima occorrenza della sottostringa.
  • Se la sottostringa non viene trovata, restituisce string::npos(string::pos è un membro statico con il valore più alto possibile per il tipo di dati size_t).

Analisi della complessità:

    Complessità temporale: O(N*M), dove N è la dimensione della stringa(str) e M è la dimensione della sottostringa da cercare. Spazio ausiliario: O(1)

Esempio:

C++






// C++ program to demonstrate> // working of string.find()> #include> #include> using> namespace> std;> // Driver code> int> main()> {> >string str =>'geeksforgeeks a computer science'>;> >string str1 =>'geeks'>;> > >// Find first occurrence of 'geeks'> >size_t> found = str.find(str1);> >if> (found != string::npos)> >cout <<>'First occurrence is '> <<> >found << endl;> > >// Find next occurrence of 'geeks'.> >// Note here we pass> >// 'geeks' as C style string.> >char> arr[] =>'geeks'>;> >found = str.find(arr, found+1);> >if> (found != string::npos)> >cout <<>'Next occurrence is '> <<> >found << endl;> >return> 0;> }>

>

>

Produzione

First occurrence is 0 Next occurrence is 8>

Trova l'occorrenza di un carattere

Possiamo usare la funzione find per trovare anche l'occorrenza di un singolo carattere nella stringa.

Sintassi:

java come eseguire l'override
size_t find (const char c, size_t pos = 0);>

Qui, C è il carattere da cercare.

Esempio:

C++




// C++ program to demonstrate> // working of string find> // to find occurrence of> // a character> #include> #include> using> namespace> std;> // Driver code> int> main()> {> >string str =>'geeksforgeeks a computer science'>;> >char> c =>'g'>;> >// Find first occurrence of 'g'> >size_t> found = str.find(c);> >if> (found != string::npos)> >cout <<>'First occurrence is '> <<> >found << endl;> > >// Find next occurrence of 'g'> >found = str.find(c, found + 1);> >if> (found != string::npos)> >cout <<>'Next occurrence is '> <<> >found << endl;> >return> 0;> }>

>

>

Produzione

First occurrence is 0 Next occurrence is 8>

Cerca una stringa parziale

Possiamo anche cercare una parte della stringa data.

Sintassi:

size_t find (const char *str, size_t pos, size_t n);>

Qui, N è il numero di caratteri da abbinare.

Esempio:

C++




// C++ program to demonstrate> // working of string find to> // search a string> #include> #include> using> namespace> std;> // Driver code> int> main()> {> >string str =>'geeksforgeeks a computer science'>;> > >// Only search first 5 characters> >// of 'geeks.practice'> >size_t> found = str.find(>'geeks.practice'>,> >0, 5);> >if> (found != string::npos)> >cout << found << endl;> >return> 0;> }>

tipi di test

>

>

Produzione

0>