从键盘上输入一行字符,统计其中英文字母、空格、数字和其他字符的个数
时间: 2024-06-13 21:06:26 浏览: 32
以下是统计输入字符中英文字母、空格、数字和其他字符个数的Python代码:
```python
s = input("请输入一行字符:")
letters = 0
space = 0
digit = 0
others = 0
for c in s:
if c.isalpha():
letters += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print("英文字母个数:", letters)
print("空格个数:", space)
print("数字个数:", digit)
print("其他字符个数:", others)
```
相关问题
写一个Python程序 使输入一行字符 统计其中英文字母 空格数字和其他字符的个数
当然,可以的。以下是Python程序代码:
```
str = input("请输入一行字符:")
letter = 0
space = 0
digit = 0
others = 0
for c in str:
if c.isalpha():
letter += 1
elif c.isspace():
space += 1
elif c.isdigit():
digit += 1
else:
others += 1
print("在输入中,英文字母的个数为:%d,空格的个数为:%d,数字的个数为:%d,其他字符的个数为:%d" % (letter, space, digit, others))
```
您可以将上述代码复制并粘贴到Python开发环境中,并运行它。这个程序将会让您输入一行字符,并统计该字符中的英文字母、空格、数字和其他字符的个数,并在屏幕上显示出来。
c语言输入一行字符统计其中英文字母、数字、空格和其他字符的个数
以下是C语言输入一行字符统计其中英文字母、数字、空格和其他字符的个数的代码实现:
```c
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i, letters = 0, digits = 0, spaces = 0, others = 0;
printf("请输入一行字符:\n");
fgets(str, sizeof(str), stdin);
for (i = 0; str[i] != '\0'; i++)
{
if (isalpha(str[i])) // 判断是否为字母
{
letters++;
}
else if (isdigit(str[i])) // 判断是否为数字
{
digits++;
}
else if (isspace(str[i])) // 判断是否为空格
{
spaces++;
}
else // 其他字符
{
others++;
}
}
printf("字母个数:%d\n", letters);
printf("数字个数:%d\n", digits);
printf("空格个数:%d\n", spaces);
printf("其他字符个数:%d\n", others);
return 0;
}
```