从键盘输入20个字符,存放在一个字符数组中,然后分别统计其中数字、英文字母和其它字符的个数。(参考P1

从键盘输入20个字符,存放在一个字符数组中,然后分别统计其中数字、英文字母和其它字符的个数。(参考P138说明参考P232 ASCII 其中字母在字符的值在65 – 90,97 – 122 为字母,字符的值在48 – 57为数字,否则为其他字符) 用c语言编写

#include<stdio.h>
int main()
{
char str[100];
int i=0;
int num=0,ch=0,blank=0,other=0;

gets(str);
while(str[i]!='\0')
{
if((str[i]>='A' && str[i]<='Z') || (str[i]>='a' && str[i]<='z'))
ch++;//字母
else if(str[i]>='0' && str[i]<='9')
num++;//数字
else if(str[i]==' ')
blank++;//空格
else
other++;

i++;
}
printf("数字%d个,字母%d个,空格%d个,其他%d个\n",num,ch,blank,other);
return 0;
}

望采纳谢谢
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-06-04
#include<stdio.h>
main()
{
char s[20]={};
int i,na=0,nn=0,no=0;
for(i=0;i<20;i++)
{
scanf("%c",&s[i]);
if((s[i]>=65 && s[i]<=90)||(s[i]>=97 && s[i]<=122))na++;
else if(s[i]>=48 && s[i]<=57)nn++;
else no++;
}
printf("There are %d alphabets, %d numbers and %d other characters in %s\n",na,nn,no,s);
}