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;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM