How can the length of a string be determined in C language?
In C language, you can use the strlen() function to determine the length of a string. This function can be found in the
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int length;
printf("请输入一个字符串: ");
scanf("%s", str);
length = strlen(str);
printf("字符串的长度是: %d\n", length);
return 0;
}
In this example, we first define a character array called str to store the input string. We then use the scanf() function to receive the user input string. Next, we pass the string to the strlen() function to calculate its length and assign the result to the variable length. Finally, we use the printf() function to print the length of the string.
It is important to note that the strlen() function returns a value of type unsigned int, therefore when using the printf() function to output it, you need to use %u for formatting.