How to count the number of characters in a string in C language?
You can use the strlen function to count the number of characters in a string.
The prototype of the strlen function is as follows:
size_t strlen(const char *str);
In this case, the ‘str’ is the string for counting the number of characters.
Example code:
#include<stdio.h>
#include<string.h>
int main() {
char str[100];
printf("请输入一个字符串:");
scanf("%s", str);
int count = strlen(str);
printf("字符串中的字符个数为:%d\n", count);
return 0;
}
Run the code, input a string, and the program will print the number of characters in the string.