solutions/The C Programming Language/1.13.c

72 lines
1.2 KiB
C

#include <stdio.h>
#define MAX_AMOUNT_OF_LENGTHS 256
main()
{
int c, i, j;
int current_length, max_length;
int nlengths[MAX_AMOUNT_OF_LENGTHS];
for (i = 0; i < MAX_AMOUNT_OF_LENGTHS; ++i)
nlengths[i] = 0;
current_length = max_length = 0;
while ((c = getchar()) != '\n')
{
if (c == '\n' || c == ' ' || c == '\t')
{
++nlengths[current_length];
if (current_length > max_length)
max_length = current_length;
current_length = 0;
}
else
{
++current_length;
}
}
++nlengths[current_length];
if (current_length > max_length)
max_length = current_length;
printf("horizonal:\n");
for (i = 1; i < MAX_AMOUNT_OF_LENGTHS; ++i)
{
int counter = nlengths[i];
if (counter > 0)
{
printf("%d: ", i);
while (counter > 0)
{
--counter;
printf("*");
}
printf("\n");
}
}
printf("\n\nvertical:\n");
for (j = max_length; j > 0; --j)
{
for (i = 1; i < MAX_AMOUNT_OF_LENGTHS; ++i)
{
if (nlengths[i] > 0)
{
if (nlengths[i] >= j)
printf("*");
else
printf(" ");
}
}
printf("\n");
}
for (i = 1; i < MAX_AMOUNT_OF_LENGTHS; ++i)
{
if (nlengths[i] > 0)
printf("%d", i);
}
printf("\n");
}