forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexercise_1-14.c
54 lines (45 loc) · 1.04 KB
/
exercise_1-14.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <stdio.h>
#define NUM_CATEGORIES 4
#define LINES 0
#define TABS 1
#define SPACES 2
#define CHARS 3
void print_histogram_row(const char* name, int frequency)
{
printf("%-10s: ", name);
for (int i = 0; i < frequency; ++i)
{
printf("*");
}
printf("\n");
}
int main()
{
int frequencies[NUM_CATEGORIES];
for (int i = 0; i < NUM_CATEGORIES; ++i)
{
frequencies[i] = 0;
}
char current_char = EOF;
while ((current_char = getchar()) != EOF)
{
if (current_char == '\n')
{
++frequencies[LINES];
}
else if (current_char == '\t')
{
++frequencies[TABS];
}
else if (current_char == ' ')
{
++frequencies[SPACES];
}
++frequencies[CHARS];
}
print_histogram_row("Lines", frequencies[LINES]);
print_histogram_row("Tabs", frequencies[TABS]);
print_histogram_row("Spaces", frequencies[SPACES]);
print_histogram_row("Characters", frequencies[CHARS]);
return 0;
}