通过此题掌握C标准中头文件<ctype.h>
头文件:<ctype.h>
函数:
(1)int isalpha(int ch);
判断ch是否为字母,如果是返回非0,反之返回0;
(2)int isdigit(int ch);
判断ch是否为数字,如果是返回非0,反之返回0;
(3)int islower(int ch);
判断ch是否为小写字母,如果是返回非0,反之返回0;
(4)int isupper(int ch);
判断ch是否为大写字母,如果是返回非0,反之返回0;
#include <stdio.h>
#include <ctype.h>
int main()
{
int letter = 0,number = 0,blank = 0,others = 0,c; //分别为字母、数字、空格、其他
while((c = getchar()) != '\n')/*注意这里的括号不能丢,因为!=的优先级比=高,如果去掉了外面的括号就等价于 c = (getchar()!='\n');*/
{
if(isalpha(c))//判断是否为字母
letter++;
else if(isdigit(c)) //判断是都为数字
number++;
else if(c == ' ') //判断是否为空格
blank++;
else //其他
others++;
}
printf("%d %d %d %d\n",letter,number,blank,others);
return 0;
}
当然也可以不用
#include<stdio.h>
int main()
{
int letter = 0,number = 0,blank = 0,others = 0,c; //分别为字母、数字、空格、其他
while((c = getchar()) != '\n'){
if(c >= 'A' && c<='Z' || c >= 'a' && c <= 'z') //判断是否为字母
letter++;
else if(c >= '0' && c <= '9') //判断是都为数字
number++;
else if(c == ' ') //判断是否为空格
blank++;
else //其他
others++;
}
printf("%d %d %d %d\n",letter,number,blank,others);
return 0;
}
PS:我还是最怕指针和字符串大哭 没关系 多做 干碎自己