storing words in a trie from dictionary - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

storing words in a trie from dictionary

mecca5mecca5 Posts: 2Member
Hello guys! Am trying to read words from a dictionary and store them in a trie. I fgets a line at a time convert the first character to a hex equiv(i.e a = 61) subtract that from the character and whats returned is mapped to a key so a, b, c will return 0,1,2 then those get mapped to 2. The problem is i get segmentation default whens it comes to the loop storing the word(not so sure its this loop causing problems but most likey, as it doens't fflush to stdout). i dunno which pointer is pointing to inaccessible memory but any clues on wat am doing wrong will be appreciated please


[code]#include
#include
#include
#include
#include "trie.h"


int main(int argc, char *argv[])
{
int i;
char word[50];
char wrd[50];
char keyseq[45];


struct Trie *level;
struct Trie *t = (struct Trie*)malloc(sizeof(struct Trie));
t->nodeWord = NULL;
//printf(" i'm here 4
"); fflush(stdout);
for(i=0; i<10; i++)
t->next[i] = NULL;

level = t;
FILE *input;
input = fopen(argv[1], "r");
//printf(" i'm here 4
"); fflush(stdout);
if(input == NULL){ printf("file didnt open");}

while(fgets(word, 50, input))//copy words from dictionary
{
for(i=0; inext[ch_equiv(word[i])] == NULL)
level->next[ch_equiv(word[i])] = (struct Trie*)malloc(sizeof(struct Trie));
level = level->next[ch_equiv(word[i])];
}
//printf(" i'm here 4
"); fflush(stdout);
while(level->nodeWord != NULL)
{
for(i=0; inext[0] == NULL)
{
//copy the word into the wordNode variable
level->next[0] = (struct Trie*)malloc(sizeof(struct Trie));
level = level->next[0];


level->nodeWord = (char*)malloc(sizeof(char)*(strlen(word)+1));
level->nodeWord = strncpy(wrd, word, 45);
printf(" i'm here 4
"); fflush(stdout);
}
}
} /*end while 2*/
}/*end while 1*/

printf(" Enter "exit" to quit.
Enter Key Sequence (or "#" for next word):
");
scanf("%s", keyseq);
printf(" i'm here 4
"); fflush(stdout);
if(keyseq != NULL)
{
for(i = 0; i < strlen(keyseq); i++)
//compare the scanned string(keyseq) with keys in the tree an print that word
if((keyseq[i] == ch_equiv(level->nodeWord[i])) && level->nodeWord[i] != '')
printf("%s", level->nodeWord);
level = level->next[i];
printf(" i'm here 4
"); fflush(stdout);
}
else if(argv[1] == "#")
printf("failed");
}/* end main */


[/code]
Sign In or Register to comment.