C String Length: Using strlen() Function
In C language, you can use the strlen() function to determine the number of characters in a string. The strlen() function returns an integer representing the length of the string, which is the count of characters (excluding the null character ‘\0’). Here is an example:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
int len = strlen(str);
printf("字符串的长度为:%d\n", len);
return 0;
}
In the example above, we defined a string “Hello World”, used the strlen() function to get its length, and finally printed it out. The output result will be:
字符串的长度为:11