c语言,输入一个字符串(只包含小写字母),请输出该字符串中每个字母出现次数的统计图

输入一个字符串(只包含小写字母),请输出该字符串中每个字母出现次数的统计图,要求如图。
求完整代码,不知道怎么输出竖着的,很疑惑。

img

思路:

其实代码都是一行一行打的, 没有竖着打的, 不同的图像就是格式控制的问题。
有了基本思路然后一直不断的实验, 直到能打出自己想要的图形。

代码:
#include <stdio.h>
#include <string.h>

int main()
{
    int n;
    scanf("%d", &n);
    if(n>100) return -1;  // 条件: n<=100
    char str[100];
    memset(str, 0x00, sizeof(str));  // 用0初始化str
    scanf("%s", str);

    int words[26][1];
    memset(words, 0x00, sizeof(words)); // 用0初始化words
    char max_words = (char)96;    // 用来记录最后一个字母是哪个。
    char min_words = (char)123;   // 用来记录第一个字母
    int  max_count = 0;           // 统计哪个字母重复的个数最多, 个数是多少。

    // 统计字母个数:
    for(int i=0; i<strlen(str); i++)
    {
        int tmp = str[i]-97;
        if(str[i]) words[tmp][0]+=1;
        if(max_count < words[str[i]-97][0]) max_count = words[str[i]-97][0];
        if(max_words < str[i]) max_words = str[i];
        if(min_words > str[i]) min_words = str[i];
    }

    for(int j=min_words; j<=max_words; j++)
    {
        printf("%d->", words[j-97][0]);
    }
    printf("\n");

    // 输出。
    int max_count1 = max_count;
    for(int i=0; i<max_count+2; i++)   // 纵行max_count+2行: max_count+最后两行。
    {
        int nn = 1;
        int nn1 = 1;
        char min_words1 = min_words;
        char max_words1 = max_words;
        for(int j=1; j<=((int)max_words-96)*3; j++)  // 字母或字母左右空格共三个位置。
        {
            int an = 2 + 3 * (nn - 1); // an是等差数列第n项。因为n在前面用过了, 所以这里用了nn代替n。
            int an1 = 2 + 3 * (nn1 - 1); // an是等差数列第n项。因为n在前面用过了, 所以这里用了nn代替n。
            if(i == max_count) // 输出----------- 每个字母占三个-
            {
                printf("-");
            }
            if(i == max_count+1)  // 输出字母, 每个字母前后都有空格
            {
                if(j == an1)
                {
                    if(min_words<=max_words)
                    {
                        printf("%c", (char)(min_words));
                        min_words++;
                        nn1++;
                    }
                }
                else printf(" ");
            }
            if(i<max_count)     //输出------上面的内容
            {
                // 输入=的位置是2,5,8,11,14...是一个等差数列:an=a1+3*(nn-1), 其中a1=2.
                if(j == an)
                {
                    if(words[min_words1-97][0] > 0)
                    {
                        if(words[min_words1-97][0] == max_count1)
                        {
                            printf("=");
                            words[min_words1-97][0] -=1;
                        }
                        else printf(" ");
                    }
                    else printf(" ");
                    nn++;
                    min_words1++;
                }
                else printf(" ");
            }
        }
        max_count1--;
        printf("\n");
    }
    return 0;
}

#include <stdio.h>
#include <Windows.h>//windows编程头文件
#include <conio.h>//控制台输入输出头文件

//将光标移动到控制台的(x,y)坐标点处
void gotoxy(int x, int y)
{
    COORD coord;
    coord.X = x;
    coord.Y = y;
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

int main(int argc,char *argv[])
{
    int count[26] = {0};
    int i = 0,j = 0;
    int max = 0;
    int n = 1;

    scanf("%d",&n);//输入n个字母

    char *str  = (char *)malloc(n*sizeof(char));

    scanf("%s",str);
    
    //使用查表法统计 count[0] 用来统计a count[1]统计b .....
    //a~z 自然有序  如果 str[i]='a',则str[i] - 'a' = 0 ,a对应的下标为0
    while(str[i] != '\0')
    {                     
        if(str[i]>='a'&&str[i]<='z')//默认只统计小写
            count[str[i]-'a']++;
        i++;    
    }

    for( i = 0; i < 26 ; i++)//找出最大纵坐标
        if(count[i] > max)
            max = count[i];

        max += 4;//最大高度加4,可以使竖状图下移4格,纯粹为了好看


    printf("\n\n\n");

    for(i = 0; i < 26 ; i++)//打印竖状图
    {
        gotoxy(i*3+4, max);//设置x方向的起点为4,每个字母占三格,最大高度为max
        printf("%c " , (char)('a'+i));//a~z a为第一个小写字母,依次加i便可以得出a、b、c....

        gotoxy(i*3+4, max-1);//字母上方打印横线
        printf("----");

        for( j = 0; j< count[i] ; j++ )//打印字母上方的 "="
        {
            
            gotoxy(i*3+4, max - j - 2 );
            printf("=");
        }
    
    }

    printf("\n\n\n");
    

    return 0;
}

img

适合window下面的编辑环境

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int n,i,j,count=0;
    int lst[26]={0}; 
    char* a;
    scanf("%d",&n);
    a=(char*)malloc(sizeof(char)*n);
    scanf("%s",a);
    for(i=0;i<n;i++){
        lst[a[i]-97]++;
        count++; 
    }
    for(i=0;i<n;i++){
        if(lst[i]>count){
            count=lst[i];
        }
    }
    for(i=count;i>=1;i--){
        for(j=0;j<26;j++){
            if(lst[j]>=i){
                printf(" = ");
            }else{
                printf("   ");
            }
        } 
        printf("\n");
    }    
    printf("------------------------------------------------------------------------------\n");
    printf(" a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z ");
    return 0;
}