Someone experienced that understands well the manipulation of Strings, that can help in this error

-1

Good morning, I'm presenting a problem that I have no idea why, I've tried to ask other people, none have discovered the real reason for the error and how to fix it. Well my teacher, asked to implement a system with which opens a txt file that contains words and these words should be stored in a tree trie. The implementation of this tree with a test is available at: link

In this way, the implementation of this tree is very close to what needs to be done, since through a string array the words are inserted in the tree. Below the main function of the implementation that I provided the link, the output of this program is correct, indicating that there is no word "these" and there is the word "the"

[

SobasicallyIadaptedthecodebyreadingthetxtfileanditdidnotwork,Ibelievethattheproblemmayberelatedtotheinsertmethod.Ididatesttoo,printingallthewordsinthefileandthankfullyitworked,ortheproblemisjustenteringthewords

It does not make sense that all words are right as you go. And at the time of saying whether or not that word in the tree does not work, I used the same implementation that was working correctly. Thank you very much if anyone knows what I did wrong

    
asked by anonymous 13.11.2018 / 12:32

1 answer

0

First of all I recommend always posting the code itself and not the image, it makes it easier to try to simulate and help with the solution. I tried to reproduce your code and realized that the error is in the regex regular expression. Actually the expression you defined nor was able to run in my test (I'm using visual studio 2017, I do not know if it has problem for the environment). Anyway I was able to execute in the expected way using the code below, where I had to change the regex regular expression.

int main()
{
    // Input keys (use only 'a' through 'z' 
    // and lower case) 
    string subject = "the a there answer any by bye their";

    struct TrieNode *root = getNode();

    // Construct trie 
    regex re = regex("\w+");
    sregex_iterator next(subject.begin(), subject.end(), re);
    sregex_iterator end;
    int i = 0;

    while (next != end) {

        smatch match = *next;
        string palavra = match.str();
        replace(palavra.begin(), palavra.end(), '[', '
int main()
{
    // Input keys (use only 'a' through 'z' 
    // and lower case) 
    string subject = "the a there answer any by bye their";

    struct TrieNode *root = getNode();

    // Construct trie 
    regex re = regex("\w+");
    sregex_iterator next(subject.begin(), subject.end(), re);
    sregex_iterator end;
    int i = 0;

    while (next != end) {

        smatch match = *next;
        string palavra = match.str();
        replace(palavra.begin(), palavra.end(), '[', '%pre%');
        replace(palavra.begin(), palavra.end(), ']', '%pre%');

        cout << palavra << " " << i << endl;
        insert(root, palavra);
        next++;
        i++;
    }

    // Search for different keys 
    search(root, "the") ? cout << "Yes\n" :
        cout << "No\n";
    search(root, "these") ? cout << "Yes\n" :
        cout << "No\n";
    return 0;
}
'); replace(palavra.begin(), palavra.end(), ']', '%pre%'); cout << palavra << " " << i << endl; insert(root, palavra); next++; i++; } // Search for different keys search(root, "the") ? cout << "Yes\n" : cout << "No\n"; search(root, "these") ? cout << "Yes\n" : cout << "No\n"; return 0; }

Follow the image of the output:

    
14.11.2018 / 17:39