0

My code just works here for speller. I am receiving 4 out of 5 in style. A 5 over 5 will make me pass this pset by crossing 70% threshold. Can you Please spot errors in style?

// Implements a dictionary's functionality
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <ctype.h>
#include "dictionary.h"

// Temporary mug for one dict words
char mug[LENGTH + 1] = {'\0'};
unsigned int track_of_dict_words = 0;
int c;
int k = 0;
int j = 0;
int index = 0;
int p = 0;

// Creating nodes
struct node *getNode(void)
{
    struct node *pNode = (struct node *)calloc(1, sizeof(struct node));
    if (pNode)
    {
        pNode -> is_word = false;
        for(int i = 0; i < 27; i++)
        {
            pNode -> children[i] = NULL;
        }
    }
    return pNode;
}

// Returns true if word is in dictionary else false
bool check(const char *word)
{
    if (root == NULL)
    {
        return 0;
    }

    struct node *curr = root;

    while (*word)
    {
        curr = curr -> children[tolower(*word) - 'a'];

        if (curr == NULL)
        {
            return 0;
        }
        word++;
    }
    return curr -> is_word;
}

// Inserting values in dictionary in the memory
void insert(const char *key)
{
    // Determining the length of the current word
    int length = 0;
    index = 0;
    for(int i = 0; i < LENGTH; i++)
    {
        if (key[i] == '\0')
        {
            break;
        }
        ++length;
    }

    struct node *pCrawl = root;

    while (*key)
    {
        if ((pCrawl -> children[tolower(*key) - 'a']) == NULL)
        {
            pCrawl -> children[tolower(*key) - 'a'] = getNode();
        }
        pCrawl = pCrawl -> children[tolower(*key) - 'a'];

        key++;
    }
    pCrawl -> is_word = true;
}

// Loads dictionary returning true if successful
bool load(const char *dictionary)
{
    root = getNode();

    // Opening dictionary file
    FILE *inptr = fopen(dictionary, "r");
    if (inptr == NULL)
    {
        fclose(inptr);
        fprintf(stderr, "Could not open %s.\n", dictionary);
        return 3;
    }
    j = 0;
    for (c = fgetc(inptr); (c != EOF); (c = fgetc(inptr)))
    {
        if ((c == '\n') || (c == EOF))
        {
            ++track_of_dict_words;
            mug[j] = '\0';
            // Inserting into Trie
            insert(mug);
            j = 0;
        }
        if (isalpha(c))
        {
            mug[j] = c;
            ++j;
        }
    }
    if (c == EOF)
    {
        printf("\nLoad Successful.");
        return true;
    }
    else
    {
        return false;
    }
}

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

// Implements unload functionality
bool flush(struct node *voot)
{
    if (voot == NULL)
    {
        return true;
    }
    for (int i = 0; i < 27; i++)
    {
        flush(voot -> children[i]);
    }
    voot = NULL;
    free(voot);
    return true;
}

// Unloads dictionary from memory, returning true if successful else false
bool unload(void)
{
    flush(root);
    //free(root);
    return true;
}
1

I see long parts of code without a comment. style50 tends to notice that too ...


When running this code through style50, I see that it complains about the first 2 for loops. Didn't it highlight anything on those lines when you ran style50 speller.c yourself?


And to quote Cliff B:

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

| improve this answer | |
  • How do we run style50 here? "style50 cs50/2018/x/speller " is not working – pankaj Oct 11 '18 at 12:11
  • style50 works local. So you shouldn't type cs50/2018/x/speller. You can use Style50 to check the style of any C-program (or Python program) you write. Simply type the name of your program after the command. So in this case: style50 speller.c – Peter Pesch Oct 11 '18 at 12:27
  • Till pset5 I called globally. My bad. I will take care of it from here. Thank you again! – pankaj Oct 11 '18 at 12:53

You must log in to answer this question.

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