题目是输入3行不超过80列的文章,求它的大写字母小写字母数字空格和其他的个数,但是运行结果显示它们的个数都是0,这是为什么(以下是我的代码)
这个问题的原因可能是您没有正确地统计字符的数量。以下是一个可以帮助您解决问题的示例代码:
#include <stdio.h>
#include <ctype.h>
int main()
{
char c;
int upper = 0, lower = 0, digit = 0, space = 0, other = 0;
printf("请输入三行文本:\n");
// 读取输入的三行文本
for(int i=0; i<3; i++){
while ((c = getchar()) != '\n') {
if (isupper(c))
upper++;
else if (islower(c))
lower++;
else if (isdigit(c))
digit++;
else if (isspace(c))
space++;
else
other++;
}
}
// 输出结果
printf("大写字母:%d\n", upper);
printf("小写字母:%d\n", lower);
printf("数字:%d\n", digit);
printf("空格:%d\n", space);
printf("其他:%d\n", other);
return 0;
}
这个代码使用了 getchar()
函数来读取输入的字符,然后使用 isupper()
、islower()
、isdigit()
、isspace()
函数来判断字符的类型。把这些字符分类统计完毕后,就可以输出结果了。