0

// Returns true if word is in dictionary else false

bool check(const char *word) { //change letters to lowercase

int length = strlen(word);

char copy[length + 1];

for (int i = 0; i < length; i++)
{
    copy[i] = tolower(word[i]);
}

int copy_index = hash(copy);

//sed pointer to the head of linked list
node *head = table[copy_index];

node *cursor = head;

if (head != NULL)

{
      while (cursor != NULL)
   {
        if (strcasecmp(copy,cursor -> word) == 0)
      {
          return true;
      }

      else
      {
         cursor = cursor -> next;
      }

    }
}

return false;

}

// Hashes word to a number// unsigned int hash(const char *word) { // TODO unsigned long hash = 5381; int c;

while ((c = *word++))
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

return hash;

}

// Loads dictionary into memory, returning true if successful else false bool load(const char *dictionary) {

FILE* source = fopen(dictionary , "r" );
// check the file is not empty
if (!source)
{
    fprintf(stderr, "Couldn't open dictionary.\n");
    return false;
}

char WORD[45];
int count = 0;

while (fscanf(source, "%s", WORD) != EOF)
{

    struct node *wnode = malloc(sizeof(node));

    if (wnode == NULL)
    {
        unload();
        return false;
    }

    strcpy(wnode->word, WORD);

    unsigned long key = hash(wnode->word);

    wnode->next = table[key];

    table[key] = wnode;

    // number of words loaded in dictionary
    count++ ;
}
return true;

}

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

// Unloads dictionary from memory, returning true if successful else false// bool unload(void) { // TODO for( int i = 0; i < N; i++) { node *head = table[i]; node *cursor = head; if ( cursor != NULL) { node *tmp = cursor; cursor = cursor -> next; free(tmp); } } return false; }

1

tried using to debugger to find the loc which results in seg fault

| improve this answer | |

You must log in to answer this question.

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