Hello!

I want to split a string, say : "I have a dog" of string data type to an array of strings.
I want to save that array.
i.e I want an array like:
array[0]="I" , array[1]="have" , array[2]= "a" , array[3]= "dog"

Please help me out.

Thanks,

Use std::istringstream

std::string str = "I have a dog" ;
std::istringstream stm(str) ;
string word ;
while( stm >> word ) // read white-space delimited tokens one by one 
{
   // put word into array
}

or boost tokenizer

std::string mid_str("I have a dog");
typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
tokenizer tokens(mid_str, boost::char_separator<char> (" "));
std::string strArray[4];
std::copy(tokens.begin(), tokens.end(), strArray);

std::string str = "I have a dog" ;
std::istringstream stm(str) ;
string word ;
while( stm >> word ) // read white-space delimited tokens one by one
{
// put word into array
}

And use a dynamic array to insert the word to the array. Use a two dimensional array if you want. Thats the most efficient way to go about it...

This question has already been answered. Start a new discussion instead.