写一函数,求一个字符串的长度(实际长度)。在main函数中输入字符串(假设其长度小于30),并输出其长度。C语言
时间: 2024-01-09 08:23:26 浏览: 134
以下是一个C语言的函数,用于求一个字符串的长度,并在main函数中输入字符串并输出其长度:
```c
#include <stdio.h>
int mystrlen(char *str) {
int len = 0;
char *ptr = str;
while (*ptr != '\0') {
ptr++;
len++;
}
return len;
}
int main() {
char buf[30];
printf("Please enter a string: ");
scanf("%s", buf);
printf("string len: %d\n", mystrlen(buf));
return 0;
}
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![txt](https://img-home.csdnimg.cn/images/20241231045021.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![text/x-c](https://img-home.csdnimg.cn/images/20250102104920.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)