strlen() prototype
size_t strlen( const char* str );
The strlen() takes a null terminated byte string str as its argument and returns its length. The length does not include the null character. If there is no null character in the string, the behaviour of the function is undefined.
It is defined in <cstring> header file.
strlen() Parameters
str: Pointer to the null terminated byte string whose length is to be calculated.
strlen() Return value
The strlen() function returns the length of the null terminated byte string.
Example: How strlen() function works
#include <cstring>
#include <iostream>
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 << endl;
if (len1 > 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;
}
When you run the program, the output will be:
Length of str1 = 13 Length of str2 = 22 str2 is longer than str1