0

My code misspells all the words of the text file.I have not made a single change to speller.c as mentioned in the instruction.I seem to have found out (using debug50) that my hash table is not getting filled .And basically there seems to be only one parent node.I don't really know why the other nodes aren't getting attached to it.Please help me out.Thank You.

typedef struct node 

{

    char word[LENGTH+1];
    struct node *next;

}node;

node *alpha[26]={NULL};

unsigned int wordcount =0;

bool loaded = false;

/**
 * Returns true if word is in dictionary else false.
 */



int hashfun(const char *s)



{

    unsigned int hash = 0;

    for (int i = 0, n = strlen(s); i < n; i++)

    {

        hash = (hash << 2) ^ s[i];

    }

    return hash % 26;



}


bool check(const char *word)

{

            int i = hashfun(word);

            node *temp =    NULL;

            temp = alpha[i];

            while(temp != NULL)

            {

                if(strcasecmp(temp->word , word) == 0)

                {

                return true;

                }


            temp = temp->next;

            }

    return false;

}



/**

 * 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];

    while(fscanf(file , "%s" , word) != EOF)

    {
      wordcount++;

      node *insert = malloc(sizeof(node));

      strcpy(insert->word, word);

      insert->next=NULL;

      int i = hashfun(word);



            if(alpha[i] == NULL)

            {

                alpha[i]=insert;

                alpha[i]->next=NULL;

            }

            else

            {

            insert->next = alpha[i]->next;

            alpha[i] = insert;

            }



    }

    loaded=true;

    fclose(file);

    return true;

}



/**

 * Returns number of words in dictionary if loaded else 0 if not yet loaded.


 */

unsigned int size(void)


{

    // TODO

    if(loaded)

    return wordcount;



    else

    return 0;

}



/**

 * Unloads dictionary from memory. Returns true if successful else false.

 */

bool unload(void)

{

    // TODO

    for(int i=0;i<26;i++)

    {

        node *cursor = alpha[i];

        while(cursor != NULL)

        {

            node *temp = cursor;

            cursor = cursor->next;

            free(temp);

        }

    }

    loaded=false;

    return true;

}
1
  • Your code is unreadable as is. You should remove all these empty lines and format it properly if you want any help. You should also describe your problem and the steps you have taken thus far to solve it, not just dump your code. See here for more details about how to ask a question and get help: cs50.meta.stackexchange.com/questions/63/… – ChrisG Jul 12 '17 at 15:17
1
fscanf(file , "%s" , word)

By design, fscanf automatically put '\0' in the end. So if it reads cat\n, the result is cat\0.

By doing this:

word[strlen(word) - 1] ='\0'

you overwrite the last char of the word. So cat\0 transforms to ca\0\0.

--Updated after comments---

Yes, there is another mistake in load(). When you add new word and try to insert new node you allocate memory first - its correct, then put the word in it - correct, then point new node to the next node - correct but then you do:

alpha[i] = insert;

basically, you replace (erase) the first node with the new node. So there are always two nodes max. Instead, you should point the first node to the new node

alpha[i]->next = insert;

It should work much better now, but there is yet another mistake in your code and I let you to identify and correct it.

2
  • I did remove the additional '\0' but still the code misspells all the words in text. Is there some other error that I am facing?I am really trying hard to figure it out. – R007 Jul 13 '17 at 9:17
  • It would be nice of you, by the way, if you'd indeed remove these empty lines as CrisG asks. Very difficult to read now. – obesman Jul 13 '17 at 10:27

You must log in to answer this question.

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