0

All tests seem to pass okay apart from the substrings test.The results of this test are :

:( handles substrings properly expected "MISSPELLED WOR...", not "MISSPELLED WOR..." Log running > ./speller substring/dict substring/text... checking for output "MISSPELLED > > > > WORDS\n\nca\ncats\ncaterpill\ncaterpillars\n\nWORDS MISSPELLED: 4\nWORDS IN DICTIONARY: 2\nWORDS > IN TEXT: 6\n"... ? Expected Output: MISSPELLED WORDS

ca cats caterpill caterpillars

WORDS MISSPELLED: 4 WORDS IN DICTIONARY: 2 WORDS IN TEXT: 6 Actual Output: MISSPELLED WORDS

ca cats caterpill caterpillar caterpillars

WORDS MISSPELLED: 5 WORDS IN DICTIONARY: 2 WORDS IN TEXT: 6

So I'm not sure why it is saying caterpillar is misspelt so any help would be really appreciated!

// Implements a dictionary's functionality

#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <ctype.h>

#include "dictionary.h"

// Represents a node in a hash table
typedef struct node
{
    char word[LENGTH + 1];
    struct node *next;
}
node;

// Number of buckets in hash table
const unsigned int N = 26;

// Hash table
node *table[N];

// initiating numberofwords
int numberofwords = 0;


// Returns true if word is in dictionary, else false
bool check(const char *word)
{
    // TODO
    int n = 0;
    int size = 0;


    char lword[LENGTH + 1];
    while(word[n] != '\0')
    {
        lword[n] = tolower(word[n]);
        n++;
    }


    int index = hash(lword);
    node *cursor = table[index];

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

        cursor = cursor->next;

    }

    return false;
}

// Hashes word to a number
unsigned int hash(const char *word)
{
    long sum = 0;
    for (int i = 0; i < strlen(word); i++)
    {
        sum += tolower(word[i]);
    }
    return (sum % N);
}

// Loads dictionary into memory, returning true if successful, else false
bool load(const char *dictionary)
{
    // TODO
    FILE *file = fopen(dictionary, "r");
    if (file == NULL)
    {
        return false;
    }

    char dword[LENGTH + 1];
    while (fscanf(file, "%s", dword) != EOF)
    {
        node *n = malloc(sizeof(node));
        if (n == NULL)
        {
            return false;
        }
        strcpy(n->word, dword);
        int index = hash(n->word);
        if (table[index] == NULL)
        {

            table[index] = n;
            numberofwords++;

        }
        else
        {
            n->next = table[index];
            table[index] = n;
            numberofwords++;
        }

    }
    fclose(file);
    return true;
}

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

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


    return true;
}

1 Answer 1

1

Ahh, the thing that makes a string a string! lword in check is not technically a string as it is not properly terminated, so the result of strcasecmp is "unreliable".

You could create a text file to match the check50 test and run it with the small dictionary. But be warned, the results may be different than check50, it may even give the correct result. It depends on what is in memory after the last character of each word.

You must log in to answer this question.

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