C ++ strlen () - biblioteka standardowa C ++

Funkcja strlen () w C ++ zwraca długość podanego ciągu.

strlen () prototyp

 size_t strlen (const char * str);

Funkcja strlen () przyjmuje jako argument ciąg znaków bajtów zakończonych wartością null i zwraca jego długość. Długość nie obejmuje znaku null. Jeśli w ciągu nie ma znaku null, zachowanie funkcji jest niezdefiniowane.

Jest zdefiniowany w pliku nagłówkowym "> plik nagłówkowy.

strlen () Parametry

str: wskaźnik do łańcucha bajtów zakończonego znakiem null, którego długość ma zostać obliczona.

strlen () Zwracana wartość

Funkcja strlen () zwraca długość łańcucha bajtów zakończonego znakiem null.

Przykład: Jak działa funkcja 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; )

Po uruchomieniu programu wynik będzie następujący:

 Długość słowa1 = 13 Długość słowa2 = 22 słowo2 jest dłuższa niż słowo1

Interesujące artykuły...