练习1-14
编写一个程序,打印输入中各个字符出现频度的直方图
#include <stdio.h>
#define LENGTH 128
int main()
{
//使用下标存储ascii码表中的所有字符
int charactor[LENGTH] = {0};
int c;
while((c=getchar()) != EOF )
{
charactor[c]++;//对应的字符加1,(数组里的值存放的是每个字符出现的数量)
}
//前32个字符是不可见字符,不输出
for(int i = 31; i<LENGTH; ++i)
{
if(charactor[i] ==0 )
continue;//如果没有值则不输出
printf("%c 字符出现的总数: %d\n",(char)i,charactor[i]);//将i转换为字符输出
}
return 0;
}
输出如下: