Skip to main content
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
Bumped by Community user
added 268 characters in body
Source Link

There is a problem with my load function which i cannot wrap my head around. I can run speller however because of this one problem, my misspelled words is always more than the actual value. What is wrong with my load? I am sure the problem is in load.

bool load(const char *dictionary)

{

root = calloc(1, sizeof(node));
total_words = 0;

FILE *fp = fopen(dictionary, "r");

if (fp == NULL)

{

printf("Could not open %s\n", dictionary); 
 return false; } 
node *trav = root; for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) { if (c == '\n') { trav->is_word = true; total_words++; trav = root; } else { int index = getIndex(c); if (trav->children[index] == NULL ) { trav->children[index] = calloc(1, sizeof(node)); } else { trav = trav->children[index]; } } } fclose(fp); return true; 

}

UPDATE*** After removing else function from if (trav->children[index]==NULL) i can run the program. Meaning i just type trav=trav->children[index] without putting it in else. But i dont understand how that makes the program work. It seems to me that its the same

There is a problem with my load function which i cannot wrap my head around. I can run speller however because of this one problem, my misspelled words is always more than the actual value. What is wrong with my load? I am sure the problem is in load.

bool load(const char *dictionary)

{

root = calloc(1, sizeof(node));
total_words = 0;

FILE *fp = fopen(dictionary, "r");

if (fp == NULL)

{

printf("Could not open %s\n", dictionary); 
 return false; } 
node *trav = root; for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) { if (c == '\n') { trav->is_word = true; total_words++; trav = root; } else { int index = getIndex(c); if (trav->children[index] == NULL ) { trav->children[index] = calloc(1, sizeof(node)); } else { trav = trav->children[index]; } } } fclose(fp); return true; 

}

There is a problem with my load function which i cannot wrap my head around. I can run speller however because of this one problem, my misspelled words is always more than the actual value. What is wrong with my load? I am sure the problem is in load.

bool load(const char *dictionary)

{

root = calloc(1, sizeof(node));
total_words = 0;

FILE *fp = fopen(dictionary, "r");

if (fp == NULL)

{

printf("Could not open %s\n", dictionary); 
 return false; } 
node *trav = root; for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) { if (c == '\n') { trav->is_word = true; total_words++; trav = root; } else { int index = getIndex(c); if (trav->children[index] == NULL ) { trav->children[index] = calloc(1, sizeof(node)); } else { trav = trav->children[index]; } } } fclose(fp); return true; 

}

UPDATE*** After removing else function from if (trav->children[index]==NULL) i can run the program. Meaning i just type trav=trav->children[index] without putting it in else. But i dont understand how that makes the program work. It seems to me that its the same

Source Link

Pset5 speller trie

There is a problem with my load function which i cannot wrap my head around. I can run speller however because of this one problem, my misspelled words is always more than the actual value. What is wrong with my load? I am sure the problem is in load.

bool load(const char *dictionary)

{

root = calloc(1, sizeof(node));
total_words = 0;

FILE *fp = fopen(dictionary, "r");

if (fp == NULL)

{

printf("Could not open %s\n", dictionary); 
 return false; } 
node *trav = root; for (int c = fgetc(fp); c != EOF; c = fgetc(fp)) { if (c == '\n') { trav->is_word = true; total_words++; trav = root; } else { int index = getIndex(c); if (trav->children[index] == NULL ) { trav->children[index] = calloc(1, sizeof(node)); } else { trav = trav->children[index]; } } } fclose(fp); return true; 

}

1
2