统计字母出现次数,打印柱状图

编写一个程序接收4行大写字母组成的字符串(每行字符串中字符的个数不能超过72),打印每个字母(不考虑空格、标点符号、分隔符和数字)在这4行字符串文本输入中出现的字数,并按照要求的输出格式显示结果。

【输入】

 共4行,每行输入的都是大写字母构成的字符串,并且每行长度不超过72

【输出】

 1行或者多行;使用*号表示字母出现的次数,其中每个大写字母之间用空格进行分割。要求每一行的结尾不要输出不必要的空格,在空行部分不要输入其他的符号(比如,等)。

【样例输入】

THE QUICKBROWN FOX JUMPED OVER THE LAZY DOG.

THIS ISAN EXAMPLE TO TEST FOR YOUR

HISTOGRAMPROGRAM

HELLO!

【样例输出】

 

仅实现主要功能。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 统计字母柱状图
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入四行英文大写字母,每行字母不超过72个:");
            string input = Console.ReadLine();
            input += Console.ReadLine();
            input += Console.ReadLine();
            input += Console.ReadLine();
            char[] inputArray = input.ToCharArray();
            char[] Letters = { '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' };
            int[] count = new int[Letters.Length];
            //统计各个字母出现的个数
            for (int j = 0; j < Letters.Length; j++)
            {
                for (int k = 0; k < inputArray.Length; k++)
                {
                    if (Letters[j] == inputArray[k])
                    {
                        count[j]++;
                    }
                }
            }

            //for (int i = 0; i < count.Length; i++)
            //{
            //    Console.WriteLine(Letters[i] + "的个数为:" + count[i]);
            //}
            //Console.ReadLine();//输出统计出的各个字母的个数
            //找出出现次数最多的字母及个数,定义一个矩阵。
            int max = count.Max();
            int row = max;
            int [,] Layout=new int[max,26];
            //给矩阵赋值,目的是为了根据矩阵输出柱状图。
            //输出柱状图,0输出空格,1输出“*”
            for (int l = 0; l < max; l++)
            {
                for (int m = 0; m < Letters.Length; m++)
                {
                    if (count[m] < row)
                    {
                        Layout[l, m] = 0;
                    }
                    else
                    {
                        Layout[l, m] = 1;
                    }                    
                }
                row--;
            }
            //输出柱状图
            for (int x = 0; x < max; x++)
            {
                for (int y = 0; y < Letters.Length; y++)
                {
                    if (Layout[x, y] == 0)
                    {
                        Console.Write("  ");
                    }
                    else
                        Console.Write("* ");
                }
                Console.WriteLine();
            }
            //输出字母
            for (int z = 0; z < Letters.Length; z++)
            {
                Console.Write(Letters[z]+" ");
            }
            Console.ReadLine();

            
           

        }
       
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值