-
Notifications
You must be signed in to change notification settings - Fork 0
/
spellmap.c
53 lines (35 loc) · 1.41 KB
/
spellmap.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "spellmap.h"
void addToMap(struct Spell * spellMap[], struct Spell *node){
int index = node->first - 'a'; // will return index in the array.
if(spellMap[index] == NULL)
spellMap[index] = node;
else {
struct Spell *current = spellMap[index];
while(current->next != NULL)
current = current->next;
current->next = node;
}
}
void readSpells(struct Spell * spellMap[], int tally[], const char * filename){
FILE *file = fopen(filename, "r");
int numberOfSpells;
fscanf(file, "%d", &numberOfSpells);
for(int i = 0; i < numberOfSpells; i++){
struct Spell *current = (struct Spell*)malloc(sizeof(struct Spell));
current->name = (char *) malloc( 30 * sizeof(char)); //assuming largest is 30
fscanf(file, "%s", current->name);
printf("%s\t\t\t", current->name);
if(i % 5 == 4) //new line every 5 words printed
printf("\n");
current->first = current->name[0];
current->last = current->name[strlen(current->name)-1];
current->next = NULL;
current->used = 0; //inititally unused
tally[current->first - 'a']++; //update tally
addToMap(spellMap, current);
}
fclose(file);
};