0

Well done all in CS50. My pset5 check function keeps listing all words as mispelled. I am using ./speller ~cs50/pset5/texts/austinpowers.txt to test run it. Please point me to where my oversight is. Below is my code, nonetheless not well commented.

CHECK FUNCTION:

bool check(const char* word)
{
    // TODO

    // determinig length of word received and creating newword to contain corrected upper to lowercase word.
    int n = strlen(word);
    char newword[n];

    for(int i = 0; i < n; i++)
        {

            if(isalpha(word[i]))
            {
                if(isupper(word[i]))
                {
                    newword[n] = tolower(word[i]);
                }
            }
            else
            {
                return false;
            }
        }

        // new node pointer to traverse linked list along derived index from hash function.
        node * traverse = buckets[hash(newword)];

        // traversing linked list for mispellings.
          while(traverse != NULL)
        {
            if(strcmp(traverse -> word, newword) == 0)
            return true;

            traverse = traverse -> next;
        }

    return false;
}

LOAD FUNCTION;

bool load(const char* dictionary)
{
    // TODO

    // initialising variable to store text scanned by fscanf
    char text [LENGTH + 1];

    // opening file to read text from
    FILE * in = fopen (dictionary, "r");

    // checking success of file opening, if not return false for failure.
    if ( in == NULL)
    {
        printf("Failed to load %s.\n", dictionary);
        return false;
    }

    else 
        {
            //while loop to load file and return true if successful
            while (fscanf(in ,"%s", text) != EOF)
            {
                // gets index for hashtable to place read text into from hash function.

                int index = hash(text);

                // creating nodes to store text. list points to first node and cursor points to nodes added to a list
                node * list = (node *) malloc(sizeof(node));

                // ensures space for first or additional nodes in heap
                if(list == NULL)
                {
                    unload();
                    return false;
                }

                //inputs text read from dictionary at start of list or in continuity to list
                else
                {
                    // import words into linked list of dictionary words.
                    strcpy(list -> word, text);
                    list -> next = buckets[index];
                    buckets[index] = list;
                }

            }
        // close file and return true for success
        fclose(in); 
        return true;
    }

    // if file not loaded return false for failure.

    return false;
}

HASH FUNCTION;

int hash(const char * string)
{
    int index = string [0] - 97;
    return index;
}

THESE ARE IN DICTIONARY.H HEADER FILE;

typedef struct node
{
    char word[46];
    struct node * next;
}node;

node * buckets[26];

// THANKS FOR ALL THE HELP. Salva

0

Since n is essentially a constant, it makes me wonder if this is what you really meant:
newword[n] = tolower(word[i]);

Also, the check function will report every word with an apostrophe as misspelled, since isalpha(word[i]) will return false in that case.

There may be other problems in the program, but correcting those issues should put you in a better place. And don't forget about debug50 and gdb.

0

Thank you very much @Dino. I appreciate the help.I got it worked out eventually under the help of @curiouskiwi the CS50 T.F.

1
  • I understood that I had to allow for a null character to be added to newword by n+1, isalpha() was un-necessary too as was the return false in the for loop. Eventually all ended well, passed check, valgrind and style. Thank you, cheers.
    – Salva Tore
    Nov 23 '16 at 22:57

You must log in to answer this question.

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