-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path025-1.13.c
37 lines (27 loc) · 882 Bytes
/
025-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
#include <stdio.h>
#define IN 1 // no new word
#define OUT 0 // new word
int main() {
int c, i, counter, wordCounter, state;
int wordLength[100];
i = counter = wordCounter = 0;
state = OUT;
while ((c = getchar()) != EOF) {
if (c == ' ' || c == '\t' || c == '\n') {
state = OUT;
--counter;
}
else if (state == OUT) { // i don't know about this warning, but it causes no issues.
state = IN;
wordLength[wordCounter] = counter;
counter = 0;
wordCounter++;
}
++counter;
}
wordLength[wordCounter] = counter; //this part exists to check the last word assuming there's no white space.
printf("\nword lengths:\n");
for(i = 1; i <= wordCounter; ++i) {
printf("Word %d length: %d\n", i, wordLength[i]);
}
}