3.输入一行字符,统计其中的英文字母、数字、空格和其他字符个数。
时间: 2023-05-18 08:05:24 浏览: 41
```python
s = input("请输入一行字符:")
letters = 0
digits = 0
spaces = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isdigit():
digits += 1
elif c.isspace():
spaces += 1
else:
others += 1
print("英文字母个数:", letters)
print("数字个数:", digits)
print("空格个数:", spaces)
print("其他字符个数:", others)
```
示例输出:
```
请输入一行字符:Hello, World! 123
英文字母个数: 12
数字个数: 3
空格个数: 2
其他字符个数: 1
```
相关问题
2. 输入一行字符,分别统计其中英文字母、空格、数字和其他字符个数。
以下是C语言实现输入一行字符,分别统计其中英文字母、空格、数字和其他字符个数的代码:
```c
#include <stdio.h>
int main() {
char c;
int y=0,s=0,k=0,z=0;
printf("请输入一串字符:");
while((c=getchar())!='\n') {
if(c<='z'&&c>='a'||c<='Z'&&c>='z') y++;
else if(c<='9'&&c>='0') s++;
else if(c==' ') k++;
else z++;
}
printf("英文字母有%d个,数字有%d个,空格有%d个,其他字符有%d个",y,s,k,z);
}
```
3.输入一行字符,分别统计其中英文字母、空格、数字和其他字符的个数。
好的,这个问题很简单,以下是实现代码:
```
def count_chars(s):
letters = 0
digits = 0
spaces = 0
others = 0
for char in s:
if char.isalpha():
letters += 1
elif char.isdigit():
digits += 1
elif char.isspace():
spaces += 1
else:
others += 1
print("字母个数为:", letters)
print("数字个数为:", digits)
print("空格个数为:", spaces)
print("其他字符个数为:", others)
```
你可以调用这个函数来统计输入字符中的数字、字母、空格和其他字符数量。例如:
```
s = input("请输入一行字符:")
count_chars(s)
```
输入一行字符后,你就可以得到相应的统计结果了。