solutions/The C Programming Language/1.13.c

74 lines
1.2 KiB
C
Raw Normal View History

2024-04-21 16:30:40 -04:00
#include <stdio.h>
2024-04-30 03:50:38 -04:00
#define MAX_AMOUNT_OF_LENGTHS 256
// drawing chart of words lengths
2024-04-21 16:30:40 -04:00
main()
{
2024-04-30 03:50:38 -04:00
int c, i, j;
int current_length, max_length;
int nlengths[MAX_AMOUNT_OF_LENGTHS];
2024-04-21 16:30:40 -04:00
2024-04-30 03:50:38 -04:00
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')
2024-04-21 16:30:40 -04:00
{
2024-04-30 03:50:38 -04:00
++nlengths[current_length];
if (current_length > max_length)
max_length = current_length;
current_length = 0;
2024-04-21 16:30:40 -04:00
}
else
2024-04-30 03:50:38 -04:00
{
++current_length;
}
}
++nlengths[current_length];
if (current_length > max_length)
max_length = current_length;
2024-04-21 16:30:40 -04:00
2024-04-30 03:50:38 -04:00
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");
2024-04-21 16:30:40 -04:00
}