c语言输入一个字符串,统计其字母,数字和其他字符的个数,并以柱状图输出
#include<stdio.h>
int main(int arge,char *argv[])
{
char ch;
int alp=0,num=0,oth=0,len,alp_start,num_start,oth_start,i;
while((ch=getchar())!=-1)
{
if(ch>='0'&&ch<='9') num++;
else if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
alp++;
else
oth++;
}
len=alp>num?(alp>oth?alp:oth):(num>oth?num:oth);
alp_start=len+1-alp;
num_start=len+1-num;
oth_start=len+1-oth;
printf("alp: %d num: %d oth: %d\n",alp,num,oth);
printf("%5s%8s%5s%8s%5s\n","alp"," ","num"," ","oth");
for(i=0;i<len+1;i++)
{
if(i==alp_start)
printf("%3d%2s",alp," ");
else if(i>alp_start)
printf("*****");
else
printf(" ");
printf("%8s", " ");
if(i==num_start)
printf("%3d%2s",num," ");
else if(i>num_start)
printf("*****");
else
printf(" ");
printf("%8s", " ");
if(i==oth_start)
printf("%3d%2s",oth," ");
else if(i>oth_start)
printf("*****");
else
printf(" ");
printf("%8s", " ");
printf("\n");
}
return 0;
}