-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwordcount.c
42 lines (33 loc) · 1014 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
38
39
40
41
42
/**
* @file htab_init.c
* @author Samuel Stolarik (xstola03)
* @brief 2nd part of DU2 IJC
*
* @date 2022-04-10
*
*/
#include "htab.h"
#include <stdio.h>
#define MAX_LEN 4096
#define SIZE_START 11519
//chose number 11519 because it is prime and I consider it suitably high
//prime numbers are better because: "every integer that shares a common factor with the length will be hashed into an index that is a multiple of this factor."
//from https://medium.com/swlh/why-should-the-length-of-your-hash-table-be-a-prime-number-760ec65a75d1
extern int read_word(char *s, int max, FILE *f);
void print_pair(htab_pair_t * data);
int main(){
htab_t * tab = htab_init(SIZE_START);
char word[MAX_LEN];
while (read_word(word, MAX_LEN, stdin) != EOF)
{
htab_lookup_add(tab, word);
}
htab_for_each(tab, (*print_pair));
htab_free(tab);
return 0;
}
void print_pair(htab_pair_t * data)
{
printf("%s \t %d\n", data -> key, data -> value);
return;
}