如何统计节点中的单词数?

How to count the number of words in a node?

我正在研究 cs50 pset5 拼写器,我正在研究大小函数,您必须在其中计算字典中的单词数。我正在尝试计算一个节点的每个部分有多少东西(参见 ),然后将它们加在一起得出总数。但我不知道该怎么做。到目前为止,这是我的代码:

// Implements a dictionary's functionality

#include <ctype.h>
#include <stdio.h>
#include <sys/resource.h>
#include <sys/time.h>
#include <string.h>
#include <stdlib.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];

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

// Hashes word to a number
unsigned int hash(const char *word)
{
    // TODO
    for(int i = 65; i < N - 1 + 65; i++)
    {
        char c = i;
       if(word[0] == c)
       {
           return i;
       }

    }
    return 0;
}

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

     }
     FILE* word2 = NULL;
     char* wordread = NULL;
     fopen("r", dictionary);

     while(wordread)
     {

     fscanf(word2, "%s",wordread );
     node *n = malloc(sizeof(node));
     strcpy(n->word, wordread);
     n->next = NULL;
     const char *word = wordread;
     
    unsigned int hash(const char *word);
 

     }

    return false;
}

// Returns number of words in dictionary if loaded else 0 if not yet loaded
unsigned int size(void)
{
   int count = 0;
    // TODO
     for(int i = 0; i < 26; i++)
    {
        for(int j = 0; j < strlen(table[i]); j++)
        {
            count++;
        }
    }
    return 0;
}

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

关注大小函数。我只添加了其他代码,以便您可以理解一些变量。我试着用谷歌搜索,但这个问题非常具体,我找不到任何东西。这也是一个问题,我必须保持函数定义不变,例如我不能从这里更改大小函数:

unsigned int size(void)

对此:

unsigned int size(const char *dictionary)

如果您对我如何修复我的代码有任何想法,请告诉我,即使错误是在不同的函数中。 谢谢

我想你必须把你的 const char *dictionary 发送到 size

那么我希望你的双字母表是字母表中每个字母的char[26],首先验证他是否被完美分配;)

我还希望你的每个字母都有一个二维数组。

for(int i = 0; dictionary[i]; i++) // mean : while dictionnary isn't NULL
{
    for(int j = 0; j < dictionary[i][j]); j++) // mean : while every word of the letter given by the index i are readed
    {
        count++;
    }
}