This is the K&R Exercise 1-14 of C. The exercise ask you to print a histogram of the frequencies of different characters in input. How can I improve my code? The main doubt is the part when I have to increase letters in the array.
#include <stdio.h>
#include <ctype.h>
int main()
{
int ch,
nwhite,
nother;
int num[10] = {0};
int lett[26] = {0};
nwhite = nother = 0;
while ((ch = getchar()) != EOF)
{
if (isdigit(ch))
++num[ch-'0'];
else if (isalpha(ch))
{
if(ch >='A' && ch <= 'Z')
ch = tolower(ch);
if(ch >= 'a' && ch <= 'k')
++lett[ch-'a'];
else if(ch >= 'l' && ch <= 'v')
++lett[ch-'l'];
else if(ch >= 'w' && ch <= 'z')
++lett[ch-'w'];
}
else if (ch == ' ' || ch == '\n' || ch == '\t')
++nwhite;
else
++nother;
}
putchar('\n');
printf("Numbers|");
for(int i = 0; i <= 10; ++i)
{
for(int j = 0; j < num[i]; ++j)
putchar('*');
}
putchar('\n');
printf("Letters|");
for(int i = 0; i <= 10; ++i)
{
for(int j = 0; j < lett[i]; ++j)
putchar('*');
}
putchar('\n');
printf("White spaces|");
for(int i = 0; i <= nwhite; ++i)
{
if(nwhite - i > 0)
putchar('*');
}
putchar ('\n');
printf("Others|");
for(int i = 0; i <= nother; ++i)
{
if(nother - i > 0)
putchar('*');
}
putchar ('\n');
return 0;
}