#include <stdio.h>
#include <assert.h>
int strlen(const char* s){
assert(s!=NULL);
int len=0;
while(*s!='\0'){
++len;
++s;
}
return len;
}
int main() {
char string[1010];
scanf("%s", string);
printf("%d\n", strlen(string));
return 0;
}
strlen
函数返回的是字符串的长度,即直到但不包括第一个 \0
字符的字符数。如果字符的个数(不包括 \0
)正好等于字符数组的大小,那么 strlen
会正确返回这个长度(即数组大小减一,因为数组大小包括了 \0
)