输入一个字符串,统计输入字符中数字、大写字母、小写字母、其他字符的个数,并输出,
时间: 2023-12-12 14:59:51 浏览: 61
示例输入:Hello World! 123
示例输出:
数字:3
大写字母:2
小写字母:8
其他字符:3
代码实现:
s = input("请输入一个字符串:")
num_count = 0 # 统计数字个数
upper_count = 0 # 统计大写字母个数
lower_count = 0 # 统计小写字母个数
other_count = 0 # 统计其他字符个数
for c in s:
if c.isdigit():
num_count += 1
elif c.isupper():
upper_count += 1
elif c.islower():
lower_count += 1
else:
other_count += 1
print("数字:%d" % num_count)
print("大写字母:%d" % upper_count)
print("小写字母:%d" % lower_count)
print("其他字符:%d" % other_count)
相关问题
输入一个字符串,统计输入字符串中数字、大写字母、小写字母、其他字符的个数,并输出。
好的,这是一个常见的字符串处理问题。您可以使用循环遍历字符串中的每个字符,然后通过判断每个字符的 ASCII 码值来确定它属于哪个类别。以下是代码示例:
#include <stdio.h>
int main() {
char str[100];
int digits = 0, uppercase = 0, lowercase = 0, others = 0;
printf("请输入一个字符串:");
gets(str);
for (int i = 0; str[i]; i++) {
if (str[i] >= '0' && str[i] <= '9') {
digits++;
} else if (str[i] >= 'A' && str[i] <= 'Z') {
uppercase++;
} else if (str[i] >= 'a' && str[i] <= 'z') {
lowercase++;
} else {
others++;
}
}
printf("数字个数:%d\n", digits);
printf("大写字母个数:%d\n", uppercase);
printf("小写字母个数:%d\n", lowercase);
printf("其他字符个数:%d\n", others);
return 0;
}
希望这个代码能够帮到您!
输入一个字符串,统计输入字符串中数字、大写字母、小写字母、其他字符的个数,并输出。写一个程序
好的,这个问题可以回答。以下是示例代码:
string = input("请输入一个字符串:")
digits, uppercase, lowercase, other = 0, 0, 0, 0
for char in string:
if char.isdigit():
digits += 1
elif char.isupper():
uppercase += 1
elif char.islower():
lowercase += 1
else:
other += 1
print("输入字符串中数字的个数为:", digits)
print("输入字符串中大写字母的个数为:", uppercase)
print("输入字符串中小写字母的个数为:", lowercase)
print("输入字符串中其他字符的个数为:", other)
当然,如果您想继续与我进行对话,请提出更多问题。
相关推荐













