C ++ strlen () - Libreria standard C ++

La funzione strlen () in C ++ restituisce la lunghezza della stringa data.

prototipo strlen ()

 size_t strlen (const char * str);

Strlen () accetta una stringa di byte con terminazione null str come argomento e ne restituisce la lunghezza. La lunghezza non include il carattere null. Se non è presente alcun carattere null nella stringa, il comportamento della funzione non è definito.

È definito nel file di intestazione "> file di intestazione.

Parametri strlen ()

str: puntatore alla stringa di byte terminata da null la cui lunghezza deve essere calcolata.

strlen () Restituisce il valore

La funzione strlen () restituisce la lunghezza della stringa di byte terminata da null.

Esempio: come funziona la funzione strlen ()

 #include #include using namespace std; int main() ( char str1() = "This a string"; char str2() = "This is another string"; int len1 = strlen(str1); int len2 = strlen(str2); cout << "Length of str1 = " << len1 << endl; cout << "Length of str2 = " << len2 < len2) cout << "str1 is longer than str2"; else if (len1 < len2) cout << "str2 is longer than str1"; else cout << "str1 and str2 are of equal length"; return 0; )

Quando esegui il programma, l'output sarà:

 Lunghezza di str1 = 13 Lunghezza di str2 = 22 str2 è più lunga di str1

Articoli interessanti...