forked from mareksuscak/cs50
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionary.c
67 lines (53 loc) Β· 1.16 KB
/
dictionary.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
// Implements a dictionary's functionality
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <ctype.h>
#include "trie.h"
#include "dictionary.h"
trie_t root = NULL;
// Keeps track of the # of words in dictionary
unsigned long words = 0;
// Returns true if word is in dictionary else false
bool check(const char *word)
{
return trie_find(root, word);
}
// Loads dictionary into memory, returning true if successful else false
bool load(const char *dictionary)
{
FILE *file = fopen(dictionary, "r");
if (!file)
{
return false;
}
root = trie_create();
if (root == NULL)
{
return false;
}
char buffer[LENGTH];
while (fscanf(file, "%s", buffer) != EOF)
{
if (!trie_insert(root, buffer))
{
unload();
break;
}
words++;
}
fclose(file);
return true;
}
// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
return words;
}
// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
trie_free(root);
return true;
}