I have spent lots of time to debug but no result get. Problem from http://acm.hnu.cn:8080/online/?actio...=show&id=10190
My idea:
1) Calcuating the letter how often they appear.
2) Find the topest apearing letter.
3) If the current line number is less then current alphabet letter appearing number. Then print a asterisk, otherwise print a space. The space after the last asterisk should not be printed.
Here is my code
Code:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define MAX_S 80
int alphabet[26];
int height;
void vertHistogram()
{
int i, j, k;
for (i = height; i >= 1; i--) {
k = 0;
for (j = 0; j < 26; j++)
if (alphabet[j] >= i) k = j;
for (j = 0; j <= k; j++) {
if (alphabet[j] >= i)
printf("*");
else printf(" ");
if (j < k) printf(" ");
}
printf("\n");
}
for (i = 0; i < 26; i++) {
printf("%c", 'A'+i);
if (i < 25) printf(" ");
}
printf("\n");
}
int main()
{
char s[MAX_S];
int i, j;
for (i = 0; i < 26; i++)
alphabet[i] = 0;
for (i = 1; i <= 4; i++) {
fgets(s, sizeof s, stdin);
for (j = 0; j < strlen(s)-1; j++) {
if (isalpha(s[j]) && isupper(s[j]))
alphabet[(int)(s[j]-'A')]++;
}
}
height = 0;
for (i = 0; i < 26; i++)
if (height < alphabet[i])
height = alphabet[i];
vertHistogram();
return 0;
}