Home >Backend Development >C++ >How to use strlen function in c++
strlen function is used to calculate the length of a string, excluding the null character at the end of the string. It returns the length of the string as an integer of type size_t, without modifying the original string. Usage: size_t strlen(const char* str);, where str is a constant pointer to a string.
Usage of strlen function in C
Use of strlen function
The strlen function returns the length of the given string, excluding the null character ('\0') at the end of the string.
Function prototype
<code class="cpp">size_t strlen(const char* str);</code>
Parameters
str
: Points to the length to be calculated A constant pointer to a string. Return value
Returns the length of the string (number of bytes), excluding null characters.
Usage
When using the strlen function, you need to pass a constant pointer to a string. Here are some examples:
<code class="cpp">const char* str1 = "Hello"; size_t length1 = strlen(str1); // length1 将为 5 const char* str2 = ""; size_t length2 = strlen(str2); // length2 将为 0</code>
Note
size_t
, which represents the length of the string. The above is the detailed content of How to use strlen function in c++. For more information, please follow other related articles on the PHP Chinese website!