0

I have an array of sets of numbers and words that are all put together in one string like so:

"hello jane 7 14 1993 female"

How do I tokenize a string like this and similar strings to assign each word to a separate variable if each word separated by a space bar is to go to its own variable like so?:

 string greeting = "hello"
 string name = "jane"
 string month = "7"
 string day = "14"
 string year = "1993"
 string gender = "female"

Thank you in advance.

adamsloma
  • 179
  • 1
  • 1
  • 8

1 Answers1

0

One option is to use std::istringstream:

#include <sstream>
#include <string>

std::string str = "hello jane 7 14 1993 female";

std::string greeting, name, month, day, year, gender;

std::istringstream(str) >> greeting >> name >> month >> day >> year >> gender;

DEMO

Piotr Skotnicki
  • 46,953
  • 7
  • 118
  • 160