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,

Recommended Answers

All 6 Replies

What exactly do you need help with?

That how to split the string into array of strings

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...

Thank you so much :)
It helped

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.