本人菜鸟,求编写一个C语言程序,要求实现的功能为:

从键盘上输入一行带有英文字母的任意字符串,假定该字符串的长度不超过50,试统计出该串中所包含的每一种字母a、b、c、d的个数(字母大小写等效),并分行依次输出,每行的输出格式为“字母:个数”,如假定字母a的个数为3,则输出格式为“a: 3”。提示:请在for循环内使用switch语句分别进行字母个数的统计。

代码
#include<stdio.h>
void main(){
char a[50],*p;
int ch;
int n2=0,n4=0,n6=0,n8=0;
printf("please enter the string:");
scanf("%s",a);
for(p=a;*p!='\0';p++)
{ch=*p;
switch(ch)
{case 'a':
case 'A':n2++;break;
case 'b':
case 'B':n4++;break;
case 'c':
case 'C':n6++;break;
case 'd':
case 'D':n8++;break;}
}
printf("a:%d\n",n2);
printf("b:%d\n",n4);
printf("c:%d\n",n6);
printf("d:%d\n",n8);
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2012-10-13
int na,nb,nc,nd; //分别记录四个出现的次数
char str;
while(c=getchar()!='\n') //输出以回车结果,不受长度控制
{
if((str=='a')||(str=='A'))
{
na++;
}
if((str=='b')||(str=='B'))
{
nb++;
}
if((str=='c')||(str=='C'))
{
nc++;
}
if((str=='d')||(str=='D'))
{
nd++;
}
}

输出语句自己写吧,不要太懒了
第2个回答  2012-10-13
这个教材上肯定就有啊
相似回答