0

Please help me figure this problem out. The program outputs around 14000 words out of the 19000 words in austinpowers.

Here is my code:

    int getSpot(int letter, node* cursor) {
        if (cursor->children[letter] == NULL) {
            cursor->children[letter] = malloc(sizeof(node));
        }
        return 0;
    }
    bool check(const char* word)
    {
        node* cursor = root;
        for (int i = 0; i <  strlen(word); i++) {
            int letter =  (int)*(word + i);
            if (letter >= 65 && letter < 96) {
                letter += 32;
            }
            if (letter == 39) {
                if (cursor->children[0] == NULL) {
                    return false;
                }
            }
            else if (cursor->children[letter - 96] == NULL) {
                return false;
            }
            if (letter == 39) {
                cursor = cursor->children[0];
            } else {
                cursor = cursor->children[letter - 96];
            }
        }
        return cursor->isWord;
    }

    /**
    * Loads dictionary into memory.  Returns true if successful else false.
    */
    bool load(const char* dictionary)
    {
        FILE* file = fopen(dictionary, "r");
        if (file == NULL) {
            return false;
        }
        char word[LENGTH + 1];
        root = malloc(sizeof(node));
        node* cursor = root;
        while (true) {
            for (int i = 0; i <= LENGTH; i++) {
                word[i] = (char)(0x00);
            }
            fscanf(file, "%s", word);
            if (feof(file) != 0) {
                break;
            }
            for (int i = 0; i < strlen(word); i++) {
                if ((int)word[i] == 39) {
                    getSpot(0, cursor);
                    cursor = cursor->children[0];
                }
                else {
                    getSpot((int)(word[i]) - 96, cursor);
                    cursor = cursor->children[(int)(word[i]) - 96];
                }
            }
            SIZEofTABLE++;
            cursor->isWord = true;
        }
        return true;
    }

    /**
    * Returns number of words in dictionary if loaded else 0 if not yet loaded.
    */
    unsigned int size(void)
    {
        // TODO
        return SIZEofTABLE;
    }

    /**
    * Unloads dictionary from memory.  Returns true if successful else false.
    */
    bool unload(void)
    {
        // TODO
        SIZEofTABLE = 0;
        return true;
    }`

Thank-you in advance!!

Jon

2

You have a very narrow but very deep trie. Each word is supposed to start at the base of the trie. Where does the second word start loading in the trie? The third? After that? ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Thank-you very much!! – Jon Aug 9 '16 at 17:00

You must log in to answer this question.

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