从键盘输入任意字符,计算其中的大小写数字空格以及其他字符的个数
#include<stdio.h>
int main(void){
char c; //定义输入字符的变量
c = getchar();
int big_letter = 0;
int small_letter = 0;
int number = 0;
int space = 0;
int other = 0;
while(c != '\n'){ //c等于回车时停止循环
if(c >= 'a' && c <= 'z'){ //大小于根据acill码来比较
small_letter++;
}
else if(c >= 'A' && c <= 'Z'){
big_letter++;
}
else if(c >= '0' && c <= '9'){ //注意c是字符变量 判断条件用单引号
number++;
}
else if(c == ' '){
space++;
}
else{
other++;
}
c = getchar(); //循环条件
}
printf("big letter = %d\n",big_letter);
printf("small letter = %d\n",small_letter);
printf("number = %d\n",number);
printf("space = %d\n",space);
printf("other = %d\n",other);
return 0;
}
输出如下: