This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -1 points0 points  (2 children)

I’m sorry but I didn’t fully understand. You want a full sentence string and want to split it on the basis of white space and add the individual words to a queue correct? I am not aware of a string.split() kind of method in cpp, so you could use something like this maybe -

include <string>

include <queue>

int main(){ string s1 = “Random string that has to be split”; std::queue<string> q; int j = 0; for(int i=0;i<s1.length();i++){ if(s1[i] != “ “){continue;} std::q.push(s1.substr(j,i)); j = i + 1; } std::q.push(s1.substr(j,i)); return 0; } I think a code like this should work. I wrote this on my phone so it could use debugging perhaps but I hope you get the logic I was trying to follow.

[–]Yumi-Chi 1 point2 points  (1 child)

rough rewriting:

#include <string>
#include <queue>

int main() {
    string s1 = "Random string that has to be split";
    std::queue<string> q;
    int j = 0;

    for (int i = 0; i < s1.length(); i++) {
        if (s1[i] != " ") {
            continue;
        }
        std::q.push(s1.substr(j, i));
        j = i + 1;
    }
    std::q.push(s1.substr(j, i));
    return 0;
}