0
std::vector<std::string> tokenize(std::string str) {
    std::vector<std::string> tokens;

    /* 
      // trying to figure out how to split the string up into individual token strings
      // 
    */
    
    return tokens;
}

void testFunc() {
  std::string text= "   smoker  coffee";
  std::vector<std::string> tokens = tokenize(text);
  
  for (std::string s : tokens) {
      std::cout << s << std::endl;
  }
  
}


output:
smoker
coffee

I've tried using std::getline(...) as suggested here How to split a file lines with space and tab differentiation?

but, I don't think it worked right and was tripping up over whether or not I was using the '\t' character as a delimiter correctly.

Rumi
  • 1
  • 1
  • 3
    The input string doesn contain any tabs (`\t`). Note that `\t` is _not_ equivalent to any specific amount of spaces. Would probably be best to show us what you already tried to implement. – Lukas-T Jun 21 '21 at 11:21
  • You could use regular expressions. This is not the most efficient, but a clear and concise way to do the job. – Evg Jun 21 '21 at 11:27
  • "Cleanest" is equivalent to personal taste so there is not a perfect answer. As @churill says, your immediate problem stems from spaces. Try `std::string text= "\t\tsmoker\t\tcoffee";` instead. – Daniel Dearlove Jun 21 '21 at 11:44
  • 1
    `stringstream ss(str); string token; while(std::getline(ss, token, '\t')) tokens.push_back(token);` – Eljay Jun 21 '21 at 13:18
  • 1
    Does this answer your question? [How do I tokenize a string in C++?](https://stackoverflow.com/questions/53849/how-do-i-tokenize-a-string-in-c) – RAM Jun 21 '21 at 17:59

0 Answers0