qfedu-c-level/day8/homework/h9.c

20 lines
437 B
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 设计函数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;
}