|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 CCCS 于 2021-3-7 00:50 编辑
#include<stdio.h>
int main()
{
int i,n;
char c[10000];
int letters=0,digit=0,others=0;
n = (int)getchar(); //输入循环次数 n
for(i=1;i<=n;i++)
{
c[i] = getchar();
if(c[i]>='a'&&c[i]<='z'||c[i]>='A'&&c[i]<='Z') //判断是否为字母
letters++; //字母数量加一
else if(c[i]>='0'&&c[i]<='9') //判断是否为数字
digit++; //数字数量加一
else
others++; //其它字符加一
}
printf("letter=%d, digit=%d, other=%d\n",letters,digit,others);
return 0;
}
10 //输入循环次数为 n = 10
aZ & //}这里的输入(字母、数字、空格、回车)
09 Az //}已经有十个,但是回车后还是不能够输出还需要继续输入是为什么呢???
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
letter=4, digit=24, other=21
Press any key to continue
1 c没必要弄成数组你要是非得弄成数组下边的c是不能单独用的,要写成c[x]的格式
2getchar()输入的数字是字符,ascii码是48-57转换成整数也是48-57,所以减去48就变成和实际输入的数一样的数
3getchar()是会把缓冲区的数据传给c的,具体的可以百度getchar缓冲区,网上有一堆解释,所以要用while把缓冲区的回车去掉
如果觉得有帮助并且看懂了的话请设个最佳
|
|