按照要求编写程序:有一行文字,不超过80个字符,分别统计出其中英文大写字母、小写字母、数字、空格、及其它字符的个数。
时间: 2024-01-29 20:02:05 浏览: 25
好的,您可以输入这行文字,我来为您统计:
```python
text = input("请输入一行不超过80个字符的文字:")
upper_count = 0
lower_count = 0
digit_count = 0
space_count = 0
other_count = 0
for char in text:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
elif char.isdigit():
digit_count += 1
elif char.isspace():
space_count += 1
else:
other_count += 1
print("大写字母个数为:", upper_count)
print("小写字母个数为:", lower_count)
print("数字个数为:", digit_count)
print("空格个数为:", space_count)
print("其它字符个数为:", other_count)
```
请注意,如果您输入的文字中有中文等其它非英语字符,并不会被计算在其它字符的个数中。
相关问题
1、按照要求编写程序:有一行文字,不超过 80 个字符,分别统计出其中英文大写 字母、小写字母、数字、空格、及其它字符的个数,
好的,以下是 Python 代码实现:
```python
s = input("请输入一行文字(不超过80个字符):")
upper_count = 0
lower_count = 0
digit_count = 0
space_count = 0
other_count = 0
for c in s:
if c.isupper():
upper_count += 1
elif c.islower():
lower_count += 1
elif c.isdigit():
digit_count += 1
elif c.isspace():
space_count += 1
else:
other_count += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("数字个数:", digit_count)
print("空格个数:", space_count)
print("其它字符个数:", other_count)
```
运行结果如下:
```
请输入一行文字(不超过80个字符):Hello, 123 World!
大写字母个数: 2
小写字母个数: 8
数字个数: 3
空格个数: 2
其它字符个数: 1
```
按照要求C语言编写程序:有一行文字,不超过80个字符,分别统计出其中英文大写字母、小写字母、数字、空格、及其它字符的个数。
好的,以下是代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main() {
char str[81];
int uppercase = 0, lowercase = 0, digits = 0, spaces = 0, others = 0;
printf("请输入一行文字,不超过80个字符:\n");
fgets(str, 81, stdin);
for (int i = 0; str[i] != '\0'; i++) {
if (isupper(str[i])) {
uppercase++;
} else if (islower(str[i])) {
lowercase++;
} else if (isdigit(str[i])) {
digits++;
} else if (isspace(str[i])) {
spaces++;
} else {
others++;
}
}
printf("大写字母个数:%d\n", uppercase);
printf("小写字母个数:%d\n", lowercase);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其它字符个数:%d\n", others);
return 0;
}
```
程序中,我们使用了 `fgets` 函数来获取输入的一行字符串,然后使用 `isupper`、`islower`、`isdigit`、`isspace` 函数来判断字符的类型,最后统计个数并输出。
相关推荐
![](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://img-home.csdnimg.cn/images/20210720083646.png)