strlen 库函数用于统计字符串中字符的个数,但不包括结束符‘\0’.
- #include <stdio.h>
-
-
int strlen_test(const char *s);
-
-
int
-
main(int argc, char **argv)
-
{
-
char *s = "Hello world";
-
int len;
-
-
len = strlen_test(s);
-
-
printf("The length of %s is %d\n", s, len);
-
-
return 0;
-
}
-
-
int
-
strlen_test(const char *s)
-
{
-
const char *sc;
-
-
for (sc = s; *sc != '\0'; ++sc) {
-
/* nothing */;
-
}
-
-
return (sc - s);
-
}
注:学习指针间的运算。
阅读(400) | 评论(0) | 转发(0) |