forked from jhpy1024/CProgrammingLanguageExercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exercise_1-13.c
72 lines (61 loc) · 1.71 KB
/
exercise_1-13.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <stdbool.h>
#define MAX_WORD_LENGTH 100
/*
* This function checks if the line for the biggest word length has already been printed.
* This means that lots and lots of lines are not printed. For example if the input
* to the program is this comment, the largest word is less than 100 and so there is
* no need to keep printing until we reach 100
*/
bool biggest_word_already_processed(int word_lengths[], int current_word_length)
{
for (int i = current_word_length; i < MAX_WORD_LENGTH; ++i)
{
if (word_lengths[i] != 0)
{
return false;
}
}
return true;
}
int main()
{
/* The integer at word_lengths[i] is the number of words of length 'i'. */
int word_lengths[MAX_WORD_LENGTH];
for (int i = 0; i < MAX_WORD_LENGTH; ++i)
{
word_lengths[i] = 0;
}
char current_char = EOF;
int current_word_length = 0;
while ((current_char = getchar()) != EOF)
{
if (current_char == ' ' || current_char == '\n' || current_char == '\t')
{
if (current_word_length <= MAX_WORD_LENGTH)
{
/* We subtract one from the length because array indices begin at zero. */
++word_lengths[current_word_length - 1];
current_word_length = 0;
}
}
else
{
++current_word_length;
}
}
for (int i = 0; i < MAX_WORD_LENGTH; ++i)
{
if (biggest_word_already_processed(word_lengths, i))
{
break;
}
printf("%3d: ", i + 1);
for (int j = 0; j < word_lengths[i]; ++j)
{
printf("*");
}
printf("\n");
}
return 0;
}