After finishing writing my functions in dictionary.c, I have tried testing it out using austinpowers.txt, but the output is blatantly incorrect. (As seen in pastebin, 6259 words are incorrect.) Some correct words are misspelled while others are correctly gone past. I'm thinking there may be something wrong with my hash table, but I believe that I coded it correctly upon reading over my functions again.

Examples w/ output of function time statistics @ bottom: https://pastebin.com/XA2eW4xZ

Load Function:

bool load(const char *dictionary)
    FILE* file = fopen(dictionary, "r");
    char word[LENGTH + 1];
    while (fscanf(file, "%s", word) != EOF) {
        word[strlen(word) - 1] = '\0';
        int hash_n = hash(word);
        link_node *node = malloc(sizeof(link_node));
        if (node == NULL) {
            return false;
        strcpy(node->word_n, word);
        node->next = hash_t[hash_n];
        hash_t[hash_n] = node;
    return true;

Check Function:

bool check(const char *word)
    char cword[strlen(word) + 1];
    for (int i = 0; i < strlen(word); i++) {
        cword[i] = tolower(word[i]);
    cword[strlen(word)] = '\0';
    link_node *cursor = hash_t[hash(cword)];
    while (cursor != NULL) {
        if (strcasecmp(word, cursor->word_n) == 0) {
            return true;
        cursor = cursor->next;
    return false;

One problem is here: word[strlen(word) - 1] = '\0';

You would need to "null out" the newline if using fgets. Not so with fscanf, it does not include the newline. From man fscanf:

s Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.

newline is considered white space, as per the same doc:

A sequence of white-space characters (space, tab, newline, etc.; see isspace(3))

You must log in to answer this question.

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