编写一个程序,要求求指定字符串的长度,并且不使用strlen()函数。 输入格式: 输入在一行中给出目标字符串,假定字符串长度不超过100个字符。 输出格式: 输出目标字符串的长度(字符串的结束标志'\0'不计算在内)
时间: 2023-05-27 18:04:07 浏览: 48
```python
s = input() # 输入字符串
length = 0 # 初始化长度为0
for i in s:
length += 1 # 遍历每一个字符,长度加1
print(length-1) # 输出字符串的长度,需要减去末尾的'\0'
```
```python
# 另一种方式,使用while循环
s = input()
length = 0
while s[length] != '\0': # 当字符不是'\0'时,长度加1
length += 1
print(length)
```
相关问题
用C语言编写一个程序,要求求指定字符串的长度,并且不使用strlen()函数。 输入格式: 输入在一行中给出目标字符串,假定字符串长度不超过100个字符。 输出格式: 输出目标字符串的长度(字符串的结束标志'\0'不计算在内)
### 回答1:
```c
#include <stdio.h>
int main()
{
char str[101];
int len = 0;
// 读入字符串
scanf("%s", str);
// 计算字符串长度
while(str[len] != '\0') {
len++;
}
// 输出长度
printf("%d\n", len);
return 0;
}
```
### 回答2:
要求编写一个C语言程序,能够计算指定字符串的长度,且不能使用strlen()函数。
字符串的长度即为字符串中字符的个数。为了实现这个功能,可以通过循环遍历字符串的每一个字符,并计数其个数,直到遇到字符串的结束标志'\0'为止。
下面是具体的程序实现:
```c
#include <stdio.h>
int main() {
char str[101]; // 存储目标字符串的字符数组,长度为100,加上一个结束标志'\0'
int length = 0; // 用于记录字符串的长度
printf("请输入目标字符串: ");
scanf("%s", str);
// 遍历字符串,直到遇到结束标志'\0'
while (str[length] != '\0') {
length++;
}
printf("目标字符串的长度为:%d\n", length);
return 0;
}
```
通过上述程序,用户可以输入一个目标字符串,然后程序会遍历该字符串,计算出其长度,并输出结果。需要注意的是,程序中将字符串的最大长度限制为100个字符,超过这个长度的字符将会被截断。
### 回答3:
#include <stdio.h>
int myStrlen(char *str) {
int len = 0;
while (*str != '\0') {
len++;
str++;
}
return len;
}
int main() {
char str[101];
scanf("%s", str);
int length = myStrlen(str);
printf("%d\n", length);
return 0;
}
使用C语言编写一个类似库函数strlen()的函数,求字符串的长度,在main()函数中输入一个字符串,调用该函数后输出结果。要求函数的形参采用指针变量。
好的,这是一个简单的C语言程序,实现了类似于库函数strlen()的字符串长度计算功能:
```c
#include <stdio.h>
int my_strlen(char *str) {
int len = 0;
while (*str != '\0') {
len++;
str++;
}
return len;
}
int main() {
char str[100];
printf("请输入字符串:");
scanf("%s", str);
int len = my_strlen(str);
printf("字符串长度为:%d\n", len);
return 0;
}
```
在该程序中,我们定义了一个名为my_strlen()的函数,它的形参是一个指向字符数组的指针变量。在该函数中,我们使用了一个while循环来遍历整个字符串,每遍历一个字符就将长度加1,直到遍历完整个字符串。最后返回字符串的长度。
在main()函数中,我们首先定义了一个字符数组str,然后使用scanf()函数从标准输入中读取这个字符串。接着调用my_strlen()函数计算字符串的长度,最后输出结果。
注意,在输入字符串时,我们使用了%s格式控制符,这个控制符表示读取一个字符串,遇到空格或者换行符就停止读取。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)