0

i have made some changes but it still prints all words as misspelled.can you please take a look at it??please

define HASHTABLE_SIZE 1346

include

include

include

include

include "dictionary.h"

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

node *hashtable[HASHTABLE_SIZE];

unsigned int hash(const char* word) { int sum = 0; for(int i=0; word[i] != '\0';i++)

    sum=sum + tolower(word[i]);

return sum % HASHTABLE_SIZE;

} unsigned counter=0; /** * Returns true if word is in dictionary else false. */ bool check(const char *word) { char word2[LENGTH + 1] = {};

int size=strlen(word);

for(int i = 0;i < size;i++)
{
    word2[i] = tolower(word[i]);
}
word2[size] = '\0';

int index1=hash(word2);


node *cursor = hashtable[index1];
while(cursor != NULL)
{


    if(strcasecmp(word2,cursor->word)==0)
    {
        return true;

    }
    else
    cursor = cursor->next;
}



return false;

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

bool load(const char dictionary) { char word[LENGTH+1] = {}; FILE fp=fopen(dictionary,"r"); { if(fp == NULL) { fprintf(stderr,"couldnot open %s\n",dictionary); } while(fscanf(fp, "%s\n" , word) != EOF) { node* new_node=malloc(sizeof(node)); if(new_node == NULL) { unload(); return false; } strcpy(new_node -> word,word); int index=hash(new_node -> word);

      if(hashtable[index] == NULL)
      {
          hashtable[index] = new_node;
          new_node ->next=NULL;
          counter++;
      }
      else if(hashtable[index] != NULL)
      {
        new_node ->next = hashtable[index];
        hashtable[index] = new_node;
        new_node ->next=NULL;
        counter++;
      }

    }
}
fclose(fp);

return true; }

0

The problem could be that word2 is not properly null-terminated, so the strcasecmp does not return 0. You should attack this with debug50. Set a breakpoint at at the strcasecmp. Use a small text file, like ralph.txt. Then try debug50 ./speller texts/ralph.txt and see what happens. Pay close attention to word2.

5
  • i tried to debug.but it says couldnot open small/ralph.txt.i am so cnfused. – Indranee Saha Jun 13 '17 at 8:40
  • Sorry, debug50 ./speller texts/ralph.txt. – DinoCoderSaurus Jun 13 '17 at 9:50
  • i used the debugger.it showed that word2 is empty while word has the word 'when'.what am i doing wrong..please dino coder saurus..please help me – Indranee Saha Jun 13 '17 at 10:34
  • You need to do the thing that makes a string a string. Properly null terminate word2. Week 2 lecture "more on strings" starting around 1:16:30. – DinoCoderSaurus Jun 13 '17 at 10:49
  • i figured out my bug and solved the problem.thankyou so much for the help. – Indranee Saha Jun 13 '17 at 15:03

You must log in to answer this question.

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