-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordcount.c
37 lines (30 loc) · 726 Bytes
/
wordcount.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
// 2) wordcount.c
// Ondřej Zobal (xzobal01)
// FIT VUT v Brně
// 15.4.2022
#include "htab.h"
#include "io.h"
#include <stdio.h>
#include <stdlib.h>
#define HTAB_SIZE 5
#define LINE_LIMIT 127
void print_pair(htab_pair_t *data) {
printf("%s\t%d\n", data->key, data->value);
}
int main() {
htab_t *htab = htab_init(HTAB_SIZE);
char *word = malloc(LINE_LIMIT);
if (word == NULL) {
fprintf(stderr, "Cannot allocate memory!\n");
exit(1);
}
while(read_word(word, LINE_LIMIT, stdin) != EOF) {
if(word[0] == '\0') {
continue;
}
htab_lookup_add(htab, word)->value++;
}
htab_for_each(htab, &print_pair);
htab_free(htab);
free(word);
}