How do we calculate the length of strings in the C language?
In the C language, the length of a string can be calculated using the strlen function. The prototype of the strlen function is as follows:
size_t strlen(const char *str);
In this case, ‘str’ is the string to calculate the length of. The function returns an unsigned integer, representing the length of the string (excluding the null character ‘\0’ at the end of the string). This function can be used to calculate the length of a string, for example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int len = strlen(str);
printf("字符串的长度为:%d\n", len);
return 0;
}
The output of the program is:
字符串的长度为:13