分类统计字符个数:输入一行文字,统计其中的大写字母、小写字母、空格、数字以及其他字符各有多少。试编写相应程序。
时间: 2024-05-02 13:16:22 浏览: 26
下面是Python的实现代码:
```python
text = input("请输入一行文字:")
upper_count = 0
lower_count = 0
space_count = 0
digit_count = 0
other_count = 0
for char in text:
if char.isupper():
upper_count += 1
elif char.islower():
lower_count += 1
elif char.isspace():
space_count += 1
elif char.isdigit():
digit_count += 1
else:
other_count += 1
print("大写字母个数:", upper_count)
print("小写字母个数:", lower_count)
print("空格个数:", space_count)
print("数字个数:", digit_count)
print("其他字符个数:", other_count)
```
运行结果示例:
```
请输入一行文字:Hello World! 123
大写字母个数: 2
小写字母个数: 8
空格个数: 2
数字个数: 3
其他字符个数: 1
```
相关问题
分类统计字符个数,输入一行文字,统计其中的大写字母、小写字母、空格、数字以及其它字符各有多少
个。
Python代码如下:
```python
text = input("请输入一行文字:")
uppercase = 0
lowercase = 0
space = 0
digit = 0
other = 0
for char in text:
if char.isupper():
uppercase += 1
elif char.islower():
lowercase += 1
elif char.isspace():
space += 1
elif char.isdigit():
digit += 1
else:
other += 1
print("大写字母:", uppercase)
print("小写字母:", lowercase)
print("空格:", space)
print("数字:", digit)
print("其他字符:", other)
```
输出示例:
```
请输入一行文字:Hello, World! 2021
大写字母: 2
小写字母: 8
空格: 2
数字: 4
其他字符: 1
```
用c++编写:输入一行文字,统计其中大写字母、小写字母、空格以及数字字符的个数
好的,这是一个计算问题,我可以为您提供解答。
下面是 C 语言代码:
```
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int i, upper=0, lower=0, digit=0, space=0;
printf("请输入一行文字: ");
fgets(str, 100, stdin);
for(i=0; str[i]!='\0'; i++) {
if(isupper(str[i])) {
upper++;
}
else if(islower(str[i])) {
lower++;
}
else if(isdigit(str[i])) {
digit++;
}
else if(isspace(str[i])) {
space++;
}
}
printf("大写字母个数: %d\n", upper);
printf("小写字母个数: %d\n", lower);
printf("数字字符个数: %d\n", digit);
printf("空格个数: %d\n", space);
return 0;
}
```
您可以复制上面的代码到您的编译器中运行,输入一行文字即可得到统计结果。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)