输入一个字符串并统计其中大写字母,小写字母,数字型字符和其他字符个数,C语言
时间: 2024-12-29 10:31:22 浏览: 58
在C语言中,你可以通过遍历输入的字符串,并使用C标准库提供的isupper()
, islower()
, isdigit()
和 ispunct()
函数来判断每个字符的类别。这里是一个简单的示例程序:
#include <stdio.h>
#include <ctype.h>
int main() {
char str[100];
int uppercase = 0, lowercase = 0, digits = 0, others = 0;
printf("请输入一个字符串: ");
fgets(str, sizeof(str), 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])) { // 不是空格或其他空白字符
others++;
}
}
printf("大写字母: %d\n", uppercase);
printf("小写字母: %d\n", lowercase);
printf("数字字符: %d\n", digits);
printf("其他字符(包括标点符号): %d\n", others);
return 0;
}
这个程序会提示用户输入一个字符串,然后计算并打印出大写字母、小写字母、数字字符和非空白字符的数量。
相关推荐


















