20 lines
437 B
C
20 lines
437 B
C
|
// 设计函数my_strlen(char *str),返回str字符串的长度
|
|||
|
#include <stdio.h>
|
|||
|
|
|||
|
int my_strlen(char *str);
|
|||
|
|
|||
|
int main(void)
|
|||
|
{
|
|||
|
char str[] = "hello world";
|
|||
|
printf("len = %d\n", my_strlen(str));
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
int my_strlen(char *str)
|
|||
|
{
|
|||
|
int len = 0;
|
|||
|
while (*str++) // 当指针指向的值为 0 (ASCII 为空字符串,判断字符串结束)时,表示已经到达字符串的末尾
|
|||
|
len++;
|
|||
|
|
|||
|
return len;
|
|||
|
}
|