-
Notifications
You must be signed in to change notification settings - Fork 0
/
WordSearch.c
346 lines (280 loc) · 8.12 KB
/
WordSearch.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
//Trevor Jenkins
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "WordSearch.h"
#define DEBUG 0
void buildStrings(Dictionary *dict, WordSearchPuzzle *puzzle);
int main (void) {
int row, col, i, max;
char *str = malloc(sizeof(char) * (MAX_WORD_LENGTH + 1));
char temp[2];
Dictionary *dict = malloc(sizeof(Dictionary));
WordSearchPuzzle *puzzle = malloc(sizeof(WordSearchPuzzle));
readDictionary(dict);
readPuzzle(puzzle);
//set bigger dimension of puzzle, use for determining
//length of diagonal for() loops
if (puzzle->height > puzzle->width) {
max = puzzle->height;
} else {
max = puzzle->width;
}
temp[1] = '\0';
for (row = 0; row < puzzle->height; row++) {
for (col = 0; col < puzzle->width; col++) {
//clear array, except for char at index 0
for (i = 1; i < MAX_WORD_LENGTH + 1; i++) {
str[i] = '\0';
}
//single char strings
*str = puzzle->grid[row][col];
checkString(dict, str);
//build strings horizontally-right
for (i = 1; i < puzzle->width; i++) {
//make sure not to go too far!
if (col + i < puzzle->width) {
temp[0] = puzzle->grid[row][col + i];
str = strcat(str, temp);
checkString(dict, str);
}
}
//clear array, except for char at index 0
for (i = 1; i < MAX_WORD_LENGTH + 1; i++) {
str[i] = '\0';
}
//build strings horizontally-left
for (i = 1; i < puzzle->width; i++) {
//make sure not to go too far!
if (col - i > 0) {
temp[0] = puzzle->grid[row][col - i];
str = strcat(str, temp);
checkString(dict, str);
}
}
//clear array, except for char at index 0
for (i = 1; i < MAX_WORD_LENGTH + 1; i++) {
str[i] = '\0';
}
//build strings vertically-down
for (i = 1; i < puzzle->height; i++) {
if (row + i < puzzle->height) {
temp[0] = puzzle->grid[row + i][col];
str = strcat(str, temp);
checkString(dict, str);
}
}
//clear array, except for char at index 0
for (i = 1; i < MAX_WORD_LENGTH + 1; i++) {
str[i] = '\0';
}
//build strings vertically-up
for (i = 1; i < puzzle->height; i++) {
//why is this >=? top row of puzzle (height/width ints) hindering correct reading?
if (row - i >= 0) {
temp[0] = puzzle->grid[row - i][col];
str = strcat(str, temp);
checkString(dict, str);
}
}
//clear array, except for char at index 0
for (i = 1; i < MAX_WORD_LENGTH + 1; i++) {
str[i] = '\0';
}
//build string diagonally up-left
for (i = 1; i <= max; i++) {
if (row - i >= 0 && col - i >= 0) {
temp[0] = puzzle->grid[row - i][col - i];
str = strcat(str, temp);
checkString(dict, str);
}
}
//clear array, except for char at index 0
for (i = 1; i < MAX_WORD_LENGTH + 1; i++) {
str[i] = '\0';
}
//build string diagonally up-right
for (i = 1; i < max; i++) {
if (row - i >= 0 && col + i < puzzle->width) {
temp[0] = puzzle->grid[row - i][col + i];
str = strcat(str, temp);
checkString(dict, str);
}
}
//clear array, except for char at index 0
for (i = 1; i < MAX_WORD_LENGTH + 1; i++) {
str[i] = '\0';
}
//build string diagonally down-left
for (i = 1; i < max; i++) {
if (row + i < puzzle->height && col - i >= 0) {
temp[0] = puzzle->grid[row + i][col - i];
str = strcat(str, temp);
checkString(dict, str);
}
}
//clear array, except for char at index 0
for (i = 1; i < MAX_WORD_LENGTH + 1; i++) {
str[i] = '\0';
}
//build string diagonally down-right
for (i = 1; i < max; i++) {
if (row + i < puzzle->height && col + i < puzzle->width) {
temp[0] = puzzle->grid[row + i][col + i];
str = strcat(str, temp);
checkString(dict, str);
}
}
}//end [col] for loop
}//end [row] for loop
for (i = 0; i < dict->size; i++) {
if (dict->counts[i] > 0) {
printf("%s (%d)\n", dict->words[i], dict->counts[i]);
}
}
destroyDictionary(dict);
destroyPuzzle(puzzle);
free(str);
str = NULL;
return 0;
}
int readDictionary(Dictionary *dict) {
int row, col;
//---------------CHANGE DICTIONARY HERE!------//
FILE *ifp = fopen("dictionary.txt", "r");
//---------------CHANGE DICTIONARY HERE!------//
//use temp_array to figure out strlen()
char array[MAX_WORD_LENGTH + 1];
fscanf(ifp, "%d", &dict->size);
//malloc() first dimension of words[][]
dict->words = malloc(sizeof(char *) * (dict->size));
//return 0 if pointer remains NULL after calling malloc()
if (dict->words == NULL) {
return 0;
}
//malloc() second dimension of words[][]
for (row = 0; row < dict->size; row++) {
//set array equal to current line of dictionary.txt
fscanf(ifp, "%s", array);
//malloc() just enough space on current row of dict->words[][]
dict->words[row] = malloc(sizeof(char) * (strlen(array) + 1));
//return 0 if pointer remains NULL after calling malloc()
if (dict->words[row] == NULL) {
destroyDictionary(dict);
return 0;
}
//read in words from array into dict->words
strcpy(dict->words[row], array);
}
//malloc() counts[] array
dict->counts = malloc(sizeof(int) * dict->size);
//clear memory and return 0 if malloc() fails
if (dict->counts == NULL) {
destroyDictionary(dict);
return 0;
}
//print dictionary
if (DEBUG) {
printf("___________DICTIONARY____________\n");
for (row = 0; row < dict->size; row++) {
printf("%s\n", dict->words[row]);
}
printf("_________________________________\n\n");
}
fclose(ifp);
return 1;
}
void destroyDictionary(Dictionary *dict) {
int row;
//free 2nd dimension's pointers first
for (row = 0; row < dict->size; row++) {
free(dict->words[row]);
dict->words[row] = NULL;
}
//can now free pointer to 1st dimension
free(dict->words);
dict->words = NULL;
free(dict->counts);
dict->counts = NULL;
dict->size = 0;
//print out pointers
if (DEBUG) {
printf("____________FREE DICT____________\n");
for (row = 0; row < dict->size; row++) {
printf("dict->words[%d]: %p", row, dict->words[row]);
}
printf("dict->words: %p\ndict->counts: %p\ndict->size: %d\n", dict->words, dict->counts, dict->size);
printf("_________________________________\n\n");
}
}
int readPuzzle(WordSearchPuzzle *puzzle) {
int row, col;
scanf("%d %d", &puzzle->height, &puzzle->width);
//malloc() 1st dimension of grid[][]
puzzle->grid = malloc(sizeof(char *) * (puzzle->height));
//catch any failed malloc()
if (puzzle->grid == NULL) {
destroyPuzzle(puzzle);
return 0;
}
//malloc() 2nd dimension of grid[][]
for (row = 0; row < puzzle->height; row++) {
puzzle->grid[row] = malloc(sizeof(char) * (puzzle->width + 1));
//catch any failed malloc() calls
if (puzzle->grid[row] == NULL) {
destroyPuzzle(puzzle);
return 0;
}
}
//populate grid[][] with puzzle
for (row = 0; row < puzzle->height; row++) {
scanf("%s", puzzle->grid[row]);
}
//print puzzle
if (DEBUG) {
printf("____________PUZZLE____________\n");
for (row = 0; row < puzzle->height; row++) {
printf("%s\n", puzzle->grid[row]);
}
printf("_______________________________\n\n");
}
return 1;
}
void destroyPuzzle(WordSearchPuzzle *puzzle) {
int row;
//free 2nd dimension of grid[][] first
for (row = 0; row < puzzle->height; row++) {
free(puzzle->grid[row]);
puzzle->grid[row] = NULL;
}
//free 1st dimension pointer
free(puzzle->grid);
puzzle->grid = NULL;
puzzle->height = 0;
puzzle->width = 0;
//print out pointers
if (DEBUG) {
printf("___________FREE PUZZLE____________\n");
for (row = 0; row < puzzle->height; row++) {
printf("puzzle->grid[%d]: %p", row, puzzle->grid[row]);
}
printf("puzzle->grid: %p\npuzzle->height: %d\npuzzle->width: %d\n", puzzle->grid, puzzle->height, puzzle->width);
printf("__________________________________\n\n");
}
}
int checkString(Dictionary *dict, char *str) {
int min = 0, mid, max = dict->size-1;
while (min <= max) {
mid = min + (max - min)/2;
if (strcmp(dict->words[mid], str) < 0) {
min = mid + 1;
} else if (strcmp(dict->words[mid], str) > 0) {
max= mid - 1;
} else {
//change dict->counts index corresponding to found string
dict->counts[mid]++;
return 1;
}
}
return 0;
}