-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathfreq-count.c
76 lines (62 loc) · 1.47 KB
/
freq-count.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
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define Delimiters "+-#@()[]{}.,:;!? \t\n\r"
/// Fixme: remove when hash_table.h is included
typedef void hash_table_t;
static int cmpstringp(const void *p1, const void *p2)
{
return strcmp(* (char * const *) p1, * (char * const *) p2);
}
void sort_keys(char *keys[], size_t no_keys)
{
qsort(keys, no_keys, sizeof(char *), cmpstringp);
}
void process_word(char *word, hash_table_t *ht)
{
printf("%s\n", word);
}
void process_file(char *filename, hash_table_t *ht)
{
FILE *f = fopen(filename, "r");
while (true)
{
char *buf = NULL;
size_t len = 0;
getline(&buf, &len, f);
if (feof(f))
{
free(buf);
break;
}
for (char *word = strtok(buf, Delimiters);
word && *word;
word = strtok(NULL, Delimiters)
)
{
process_word(word, ht);
}
free(buf);
}
fclose(f);
}
int main(int argc, char *argv[])
{
hash_table_t *ht = NULL; /// FIXME: initialise with your hash_table
if (argc > 1)
{
for (int i = 1; i < argc; ++i)
{
process_file(argv[i], ht);
}
/// FIXME: obtain an array of keys to sort them
char *keys[] = { "Bb", "Dd", "Aa", "Cc", "Hh", "Ff", "Gg" };
sort_keys(keys, 7);
for (int i = 0; i < 7; ++i) puts(keys[i]);
}
else
{
puts("Usage: freq-count file1 ... filen");
}
}