ytokenize string code example

Example: tokenize string c++

//the program take input as string and delimiter is ','.
//delimiter can  be changed in line 9;

std::vector<std::string> tokenise(const std::string &str){
    std::vector<std::string> tokens;
    int first = 0;
    //std::cout<<"aditya";
    while(first<str.size()){
        int second = str.find_first_of(',',first);
        //first has index of start of token
        //second has index of end of token + 1;
        if(second==std::string::npos){
            second = str.size();
        }
        std::string token = str.substr(first, second-first);
        //axaxax,asas,csdcs,cscds
        //0123456
        tokens.push_back(token);
        first = second + 1;
    }
    return tokens;
}

Tags:

Cpp Example