c语言从键盘输入一行字符,统计其中大写字符的个数,小写字符的个数,数字的个数,以及非字母和数字的个数。请分别用for,while循环语句实现 从键盘输入一行字符,统计其中大写字符的个数,小写字符的个数,数字的个数,以及非字母和数字的个数。请分别用for,while循环语句实现
时间: 2024-06-01 20:10:13 浏览: 45
// for循环实现
#include <stdio.h>
int main() {
char c;
int upper = 0, lower = 0, digit = 0, other = 0;
printf("请输入一行字符:");
for (int i = 0; (c = getchar()) != '\n'; i++) {
if (c >= 'A' && c <= 'Z') {
upper++;
} else if (c >= 'a' && c <= 'z') {
lower++;
} else if (c >= '0' && c <= '9') {
digit++;
} else {
other++;
}
}
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("数字:%d\n", digit);
printf("其他字符:%d\n", other);
return 0;
}
// while循环实现
#include <stdio.h>
int main() {
char c;
int upper = 0, lower = 0, digit = 0, other = 0;
printf("请输入一行字符:");
c = getchar();
while (c != '\n') {
if (c >= 'A' && c <= 'Z') {
upper++;
} else if (c >= 'a' && c <= 'z') {
lower++;
} else if (c >= '0' && c <= '9') {
digit++;
} else {
other++;
}
c = getchar();
}
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("数字:%d\n", digit);
printf("其他字符:%d\n", other);
return 0;
}
阅读全文