1
   /**
 * Implements a dictionary's functionality.
 */

#include <stdbool.h>

#include "dictionary.h"
#include<string.h>
#include <stdio.h>
#include <stdlib.h>
#include<ctype.h>
int count=0;
node *root=NULL;
/**
 * Returns true if word is in dictionary else false.
 */
bool check(const char *word)
{
    node *tmp;
    tmp=root;
    for(int i=0;word[i]!='\0';i++)
    {
    int a;
    if(word[i]>='A' && word[i]<='Z')
        a=(int)word[i]-65;
    else if(word[i]>='a' && word[i]<='z')
        a=(int)word[i]-97;
    else
        a=26;
    if(tmp->children[a]==NULL)
        return false;
    else 
        tmp=tmp->children[a];
    }
    return tmp->is_word;
}

/**
 * Loads dictionary into memory. Returns true if successful else false.
 */
char name[LENGTH+1];
bool load(const char *dictionary)
{   
    node *newnode;
    FILE* file = fopen(dictionary, "r");
    if (file == NULL)
    {
        printf("Could not open dictionary.\n");
        return false;
    }
    root= calloc(1, sizeof(node));
    while (fscanf(file,"%s\n",name)>0)
    {
        count++;
        node *tmp;
        tmp=root;
        for(int i=0;name[i]!='\0';i++)
        {
            int a;
        if(isalpha(name[i]))
            a=(int)name[i]-97;
        else
            a=26;
        if(tmp->children[a]==NULL)
        {
            newnode=calloc(1, sizeof(node));
            tmp->children[a]=newnode;
            tmp=newnode;
        }
        else
            tmp=newnode;
        }
        tmp->is_word=true;
    }
    // TODO
    fclose(file);
    return true;
}

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

/**
 * Unloads dictionary from memory. Returns true if successful else false.
 */
bool unload(void)
{   if(root==NULL)
        return false;
    delNode(root);
    return true;

}
void delNode(node *tempNode)
{
    for (int i=0;i<27;i++)
    {
        if (tempNode->children[i] != NULL)
            delNode(tempNode->children[i]);
    }
    free(tempNode);

}

OUTPUT

~/workspace/pset5/speller/ (master) $ ./speller texts/ralph.txt

MISSPELLED WORDS

When
grow
up
want
to
be
principal
or
caterpillar
love
you
Principal
Skinner
Ralph
Wiggum

WORDS MISSPELLED: 15
WORDS IN DICTIONARY: 143091
WORDS IN TEXT: 20
TIME IN load: 0.19
TIME IN check: 0.00
TIME IN size: 0.00
TIME IN unload: 0.18
TIME IN TOTAL: 0.37

3
  • root node is never allocated. Commented May 7, 2017 at 20:51
  • Even after that correction, segfault still persists. Commented May 8, 2017 at 8:22
  • Allocated memory for root. I changed malloc to calloc and it now works. Now segfault disappeared but check isnt working properly Commented May 8, 2017 at 10:07

1 Answer 1

3

check:

tolower(word[i]) returns the lowercased ith character in word. You never use the value, you might want to assign it. Or remove this loop and later use tolower(word[i]) instead of word[i] when calculating a.

Instead of

    if(tmp->is_word==true)
        return true;
    else
        return false;

just do

    return tmp->is_word;

load:

Be careful with feof. This function returns non-zero only after an unsuccessful read (like fscanf). The result being false does not guarantee more content to read.

        else if(tmp->children[a]!=NULL)
        {
            tmp=newnode;
        }

makes no sense to me and should likely be

        else
        {
            tmp=tmp->children[a];
        }

size:

Is there any reason you don't just return count; without any if?

unload:

Instead of your for-loop (in which you don't unload root itself), just delNode(root)

3
  • I changed the code according to all your suggestions. But then, still the output persists. It does not correctly identify the misspelled words. I've updated the new code. Commented May 8, 2017 at 12:50
  • In load, you did not change tmp=newnode; to tmp=tmp->children[a]; where I suggested.
    – Blauelf
    Commented May 8, 2017 at 13:27
  • Thanks for the correction! :) It worked! Commented May 8, 2017 at 13:29

You must log in to answer this question.

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