Vertical Histogram
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 17713 | Accepted: 8563 |
Description
Write a program to read four lines of upper case (i.e., all CAPITAL LETTERS) text input (no more than 72 characters per line) from the input file and print a vertical histogram that shows how many times each letter (but not blanks, digits, or punctuation) appears in the all-upper-case input. Format your output exactly as shown.
Input
* Lines 1..4: Four lines of upper case text, no more than 72 characters per line.
Output
* Lines 1..??: Several lines with asterisks and spaces followed by one line with the upper-case alphabet separated by spaces. Do not print unneeded blanks at the end of any line. Do not print any leading blank lines.
Sample Input
THE QUICK BROWN FOX JUMPED OVER THE LAZY DOG. THIS IS AN EXAMPLE TO TEST FOR YOUR HISTOGRAM PROGRAM. HELLO!
Sample Output
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 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
Source
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
char w[4][100];
int a[26];
memset(w, 0, sizeof(w));
memset(a, 0, sizeof(a));
int max = 0;
gets(w[0]);
gets(w[1]);
gets(w[2]);
gets(w[3]);
for (int i = 0; i < 4; i++)
{
for (int j = 0; j < 100; j++)
{
if (w[i][j] >= 'A' && w[i][j] <= 'Z')
{
a[w[i][j] - 'A']++;
}
}
}
for (int i = 0; i < 26; i++)
if (a[i] > max)
{
max = a[i];
}
for (int i = 0; i < max; i++)
{
for (int j = 0; j < 26; j++)
{
if (j == 0)
{
if (i + a[j] >= max)
{
printf("*");
}
else
{
printf(" ");
}
}
else
{
if (i + a[j] >= max)
{
printf(" *");
}
else
{
printf(" ");
}
}
}
printf("\n");
}
for (int i = 'A'; i <= 'Z'; i++)
{
if (i == 'A')
{
printf("%c", i);
}
else
{
printf(" %c", i);
}
}
printf("\n");
return 0;
}