输入一行字符,以回车键结束输入 分别统计其中出现的大写英文字母 小写英文字母 数字字符 空格和其他字符

等5类字符出现的次数 例如 若输入:I am 20 years old! 则5类字符出现的次数分别是1、10、2、4、1
下面是我写出的程序 不知道哪里错了
#include <iostream.h>
void main()
{
char ch;
int num=0,d=0,x=0,space=0,other=0;
while((ch=cin.get())!='\n')
if(ch>='A'&&ch<='Z')
d++;
else if (ch>='a'&&ch<='z')
x++;
else if(ch>='0'&&ch<='9')
num++;
else if(ch=' ')
space++;
else other++;
cout<<"d="<<d<<endl;
cout<<"x="<<x<<endl;
cout<<"num="<<num<<endl;
cout<<"space="<<space<<endl;
cout<<"other="<<other<<endl;
}
这个过程运行完之后得如下
d=1 x=10 num=2 space=5 other=0 里面的空格字符数 和其他字符数都不对不知道为什么
哪位能帮忙解答一下啊??很急很急 万分感谢

#include <stdio.h>
int main()
{
  int letter=0,space=0,digit=0,others=0;
  char c;
  while((c=getchar())!='\n'){
   if(c==' ')
      space++;
   else if(c>='1'&&c<='9')
       digit++;
   else if((c>='a'&&c<='z')||c>='A'&&c<='Z')
       letter++;
   else others++;
  }
  printf("The number of letters is:%d\n",letter);
  printf("The number of spaces is:%d\n",space);
  printf("The number of digits is:%d\n",digit);
  printf("The number of other words is:%d\n",others);
return 0;
}

温馨提示:答案为网友推荐,仅供参考
第1个回答  2015-11-29
#include <stdio.h>
int main()
{
int letter=0,space=0,digit=0,others=0;
char c;
while((c=getchar())!='\n'){
if(c==' ')
space++;
else if(c>='1'&&c<='9')
digit++;
else if((c>='a'&&c<='z')||c>='A'&&c<='Z')
letter++;
else others++;
}
printf("The number of letters is:%d\n",letter);
printf("The number of spaces is:%d\n",space);
printf("The number of digits is:%d\n",digit);
printf("The number of other words is:%d\n",others);
return 0;
}追问

不好意思还是有几个问题不懂
1。数字的话不包括0吗?如果是1-9的话那如果有0怎么办啊?
2. 题里的是大小写字母各自的字符数是多少 如果用||的话应该只能输出字母的字符 不能区分大小写了 是吗?
3.我上面那个程序里面也是把other放在最后 可是为什么的出来就是0呢?还有我算出来的空格数比正确的多一个 为什么
麻烦你了谢谢

本回答被网友采纳
相似回答