qfedu-c-level/day11/d8.c

29 lines
679 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.

// 自定义函数 str_len计算字符串长度
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int str_len(const char *str)
{
// const 作用: 防止str指针在函数内被修改
int len = 0;
while (str[len++])
;
return len - 1; // 不需要 '\0'
}
int main()
{
char content[100] = "";
while (1)
{
printf("data: ");
scanf("%s", content);
if (content[0] == 'q')
break; // 当输入 q 时,退出循环
// printf("%s len is %d\n", content, str_len(content));
// string.h 中的 strlen 函数
printf("%s len is %ld\n", content, strlen(content));
}
return 0;
}