0
#define NUM_NODES 27
int wordCount = 0;
typedef struct trie
{
bool is_Word;
struct trie* chilluns[27];
} 
trie;
trie* current_Node;
trie* root;

/**
* Loads dictionary into memory.  Returns true if successful else false.
*/

bool load(const char* dictionary)
{
//initializing setup stuffs
int c = 0;
trie* root = calloc(1, sizeof(trie));  
trie* current_Node = root; 
FILE* file = fopen(dictionary, "r");
if(!file)
{
    return 1;
}

//loop through the trie starting at the begining of dictionary

//traverse the dictionary
while((c = fgetc(file)) != EOF)
{
// current_Node = root;
if(c == '\n')
{



//if(&current_Node != &root && c == 10)
   //{
        if (!current_Node->is_Word == true)
        {
        //in case of duplicates.
        current_Node->is_Word = true; //once line break reached end the word
        wordCount++;
        }

        current_Node = root;
}        
   // }

//    else
//     {
//        c = fgetc(file);
 //   }

for(c = fgetc(file);c != '\n' && c != EOF; c = fgetc(file))
{
    if(!current_Node->chilluns[c - 'a'])
    { 
        current_Node->chilluns[c - 'a'] = calloc(1, sizeof(trie));
        current_Node = current_Node-> chilluns[c - 'a'];
    }


}

    current_Node->is_Word = true;
    wordCount++;  

}
    fclose(file);
    return true;
}


 /**
 * Returns true if word is in dictionary else false.
 */
bool check(const char* word)
{
//trie that points to root
trie* root = calloc(1,sizeof(trie));
int i = 0;
char inchar;

// loop through the word
while(word[i] != '\0')
{

//sets the in character of  word
inchar = word[i];


//adjusts for apostrophes
if (!isalpha(inchar))
    {
       if(inchar == '\'')
       inchar = ((int)'z' + 1);
    }    
//trie that points to root
trie* current_Node = root; 
int letter = tolower(word[i]);
if(current_Node-> chilluns[letter - 'a'] == NULL)
    {

       current_Node = current_Node->chilluns[letter - 'a'];
       i++;
    }
        else
        {
            return false;
        }

} // close loop



if(inchar == '\0' && current_Node-> is_Word) // if word exists
{
return true;  
} 
else
{
    return false;
}    
}
2
  • Also in my load function I am having some difficulties – wtfbbq81 Apr 16 '15 at 16:59
  • you need to describe more of what you are asking – BooFluff May 19 '15 at 2:23
1

It looks like you have implemented a trie for your dictionary and you are trying to traverse the dictionary letter by letter to see if a word exists. In this code block...

if(current_Node-> chilluns[letter - 'a'] == NULL)
{
   current_Node = current_Node->chilluns[letter - 'a'];
   i++;
}

...the IF statement is checking to see if this is the end of a word or if the next letter in the word exists.

The problem is this: your IF statement isn't checking for another letter, it is checking for the absence of another letter. If chilluns[x] == NULL evaluates as true, there is no next node. You will always assign NULL to current_Node. This code will never assign the pointer of a child node to chilluns. If there is a child, it will instead execute the 'return false' line.

The fix should be simple. Instead of "==", use "!=" in your test.

Alternatively, you could do

if(  !(current_Node-> chilluns[letter - 'a'] == NULL)  )

This method negates the result of whatever test you want to do before acting on it.

Or, you could reverse the actions. If chilluns[x] == NULL, then return false, ELSE assign the child node.

It's always those simple things that get by. Kind of like not seeing the forest for the trees.

If this satisfies your question, please mark the question as answered. Let's keep up on forum housekeeping. ;-)

5
  • Thank you, I do have another question maybe you could answer. I am trying to use valgrind to track down an uninitialized value and run the command valgrind ./speller ~/Dropbox/"where the file is" --track-origins=yes and it gives me an error saying could not open --track-origins=yes Any ideas on what I may be doing wrong? – wtfbbq81 Apr 16 '15 at 15:58
  • Simple. --track-origins=yes is a parameter for valgrind. You put it where you would put the parameters for the program under test. The order that must be followed is this: valgrind [--valgrind options] [program being tested] [tested program parameters] – Cliff B Apr 16 '15 at 17:15
  • again thank you. Now that I changed the code to your specs it goes through the loop and finishes without a seg fault. However, it always returns false. If I keep it as == it will go through the loop. It never counts the word as misspelled. Always goes to the line: if(current_Node->is_Word == true) // if word exists then returns false. I guess i'm mixed up in what exactly i'm doing. Been starting at it for hours. I think that perhaps current_Node-> is_Word is never set. But I thought it was in the load function. Or am I not actually comparing the two words at all? – wtfbbq81 Apr 16 '15 at 19:45
  • Hi wtfbbq81. Your question was why you were losing the nodes. Your new problems are another issue that probably deserves new questions. Answers on this forum are supposed to address the questions asked and to lead the questioner towards an answer, it isn't supposed to be a 'fix my code' dropoff. Along with only the relevant code for the problem, you should be posting a specific question, useful details about what you've done or not done, and appropriate output or compiler errors. Also, please try to have only one question at a time. – Cliff B Apr 17 '15 at 7:38
  • I really didn't mean to come off that way. I apologize. I should have been more clear about what I am asking. Like I said I'd been staring at it for hours and my brain was a bit "mushy" :-) I'm starting another thread now and will try to be a bit more articulate in the new post! – wtfbbq81 Apr 17 '15 at 15:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .