Funkce strlen () v C ++ vrací délku daného řetězce.
strlen () prototyp
size_t strlen (const char * str);
Strlen () vezme jako argument null zakončený bajtový řetězec str a vrátí jeho délku. Délka nezahrnuje znak null. Pokud v řetězci není žádný znak null, chování funkce není definováno.
Je definován v hlavičkovém souboru.
strlen () Parametry
str: Ukazatel na nulový ukončený bajtový řetězec, jehož délka se má vypočítat.
strlen () Návratová hodnota
Funkce strlen () vrací délku bajtového řetězce zakončeného nulou.
Příklad: Jak funguje funkce 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; )
Když spustíte program, výstup bude:
Délka str1 = 13 Délka str2 = 22 str2 je delší než str1