Я пишу средство проверки орфографии, но у меня проблема в том, что если предыдущая длина массива больше, чем та, которую он проверяет в настоящее время, она по какой-то причине добавляет «отсутствующие» символы из предыдущего массива.
Например, проверяемое слово - «кошка», а слово перед ним - «медведь», тогда слово, через которое оно будет проходить, будет «катр». Я использовал свой инструмент отладчика, и все выглядит хорошо. Инициализация массива с помощью c [len] не работает.
Способ загрузки:
bool load(const char *dictionary)
{
// Open dictionary list
FILE *file = fopen(dictionary, "r");
if (file == NULL)
{
unload();
return false;
}
// Create a universal root node to be used to traverse through the trie
root = malloc(sizeof(node));
node *pointer = root;
int index, charCounter = 0;
char c;
// Insert words into trie
while (1)
{
// Open the file and get a new char
c = fgetc(file);
//printf("%c", c);
if (c == '\'')
{
index = N - 1;
}
else
{
index = c - case;
}
if(isalpha(c) || c == '\'')
{
if(pointer->children[index] == NULL)
{
// Allocate memory for new node
pointer->children[index] = malloc(sizeof(node));
// Set the pointer equal to that new node
pointer = pointer->children[index];
}
else
{
pointer = pointer->children[index];
}
}
else
{
pointer->isWord = true;
pointer = root;
numWords++;
}
// If end of the file, close it and stop the loop
if(feof(file))
{
fclose(file);
break;
}
charCounter++;
//printf("%i", charCounter);
}
// Indicate success
return true;
}
Метод проверки:
bool check(const char *word)
{
// Set the pointer to equal the root with every new word
node *pointer = root;
// Create array to traverse through
int len = strlen(word);
int index = 0;
char c[len];
// Create a word array with all letters lowercase
for (int i = 0; i<len; i++)
{
if (isupper(word[i]))
{
c[i] = tolower(word[i]);
}
else
{
c[i] = word[i];
}
}
// Loop to go through the every letter in the word
for (int i = 0; i<=len; i++)
{
// Index would equal the position in the node
index = c[i] - case;
//printf("%i", index);
if (c[i] == '\'')
{
index = N - 1;
}
if (c[i] != '\0')
{
// Checking basics for the location if NULL then automatically not in node so false
if (pointer->children[index] == NULL)
{
return false;
}
// Otherwise, if it's not NULL it has to be a valid entry in the node
else
{
pointer = pointer->children[index];
}
}
else
{
if (pointer->isWord == true)
{
return true;
}
}
}
return false;
}