C Language Example - Calculate String Length
Calculate string length.
Example - Using strlen()
#include <stdio.h>
#include <string.h>
int main()
{
char s[1000];
int len;
printf("Input string:");
scanf("%s", s);
len = strlen(s);
printf("String length: %d", len);
return 0;
}
The output result is:
输入字符串: example 字符串长度: 6
Example - Without Using strlen()
#include <stdio.h>
int main()
{
char s[1000], i;
printf("Input string:");
scanf("%s", s);
for(i = 0; s[i] != '\0'; ++i);
printf("String length: %d", i);
return 0;
}
The output result is:
输入字符串: example 字符串长度: 6other extensions