Search code examples
cstringrecursionstructtrie

storing several strings from a recursive function into a struct c


I am making a predictive text interface by which I store a dictionary into a data structure (ive used a trie), the user searches partially for a word and the completed words are displayed with numbers corresponding to each one. I have done the insertion, search functions and have a recursive traversal that prints out all the completed words(with no numbers). however I want to store them into a struct so that i can use them in another function where the user will then be shown the words with corresponding numbers.

heres the main.c code (for testing it does not go into readfile that enters all 25 000 words!):

struct TrieNode* root = trieRootConstructor();
struct TrieNode* pntr = NULL;

trieInsert(root, "aback");
trieInsert(root, "abacus");
trieInsert(root, "abalone");
trieInsert(root, "abandon");
trieInsert(root, "abase");
trieInsert(root, "abash");
trieInsert(root, "abate");
trieInsert(root, "abater");

int x = 0;
char* result = "";
char* search = "aba";

result = trieSearch(root, &pntr, search, result, &x);

printf("\n\n");

traverseTwo(pntr, search);

pntr is set to the node where the partial word ends, this is where the traversal will search for the rest of the word.

here is my recursive traversal and its caller:

void traverseTwo(struct TrieNode* node, char* partialWord)
{
    char arr[50];
    int index = 0;

    int maxWordSize = 100;
    char wordArr[50][maxWordSize];

    index = recursivePrint(node->children, arr, wordArr[50], 0, partialWord, index);

    int i = 0;

    for(i = 0; i < index; i++)
         printf("%d: %s\n", i, wordArr[i]);

    printf("%d: Continue Typing", index);
}

 int recursivePrint(struct TrieNode* node, char* arr, char* wordArr, int level, char* partialWord, int index)
{
     if(node != NULL)
     {
          arr[level] = node->symbol;

         index = recursivePrint(node->children, arr, wordArr, level+1, partialWord, index);

         if(node->symbol == '\0')
             index = completeWordAndStore(partialWord, arr, wordArr, index);

        index = recursivePrint(node->sibling, arr, wordArr, level, partialWord, index);
    }
    return index;
}

int completeWordAndStore(char* partialWord, char* restOfWord, char* wordArr, int index)
{
    int length = strlen(partialWord) + strlen(restOfWord);
    char completeWord[length];

    strcpy(completeWord, partialWord);
    strcat(completeWord, restOfWord);

    strcpy(wordArr[index], completeWord);

    index++;

    return index;
}

I am getting a segmentation fault on strcpy(wordArr[index], completeWord);

The idea (in my head) is that once it goes into the if statement where the nodes symbol is '\0' that is where it will store the string at the value of index.

partial word is the part word that has been searched ee.g "aba", i would strcat it with the arr and store it into the struct.

the result should produce:

0: aback 1: abacus 2: abalone 3: abandon 4: abase 5: abash 6: abate 7: abater 8: Continue typing

I do call a destructor later on but that definitely words as its tried and tested.

can anyone suggest how to modify this so that I can store the strings??

I also assume that it would be an array structure if i am correct??

many Thanks

Jack


Solution

  • char* wordArr[50];
    

    You're not allocating any memory for your words. Try with:

    int maxWordSize = 100;
    char wordArr[50][maxWordSize];