What is the usage of the strlen function in C++?

The strlen function is used to find the length of a string, which is the number of characters in the string (excluding the null character ‘\0’ at the end of the string). Its usage is as follows:

#include <cstring>

size_t strlen(const char* str);

The parameter ‘str’ is the string whose length needs to be determined, and the return value is the number of characters in that string. An example usage is shown below:

#include <iostream>
#include <cstring>

int main() {
    const char* str = "Hello, World!";
    size_t length = strlen(str);
    
    std::cout << "Length of the string: " << length << std::endl;
    
    return 0;
}

The above code will output the length of the string “Hello, World!”, which is 13.

bannerAds