// strings and c-strings
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
using namespace std;
vector<string> SplitString (string aString)
{
vector<string> vec;
char * cstr, *p;
//string str ("Please split this phrase into tokens");
string str = aString;
cstr = new char [str.size()+1];
strcpy (cstr, str.c_str());
// cstr now contains a c-string copy of str
p=strtok (cstr," ");
while (p!=NULL)
{
vec.push_back(p);
p=strtok(NULL," ");
}
delete[] cstr;
return vec;
}
int main ()
{
std::vector<std::string> vec = SplitString("Please split this phrase into tokens");
for (std::vector<std::string>::size_type a = 0; a < vec.size(); ++a)
{
cout << vec.at(a) << '\n';
}
cin.get();
return 0;
}
Last edited by iamthwee; Nov 2nd, 2009 at 12:29 pm.
Reputation Points: 1536
Solved Threads: 431
Posting Expert
Offline 5,865 posts
since Aug 2005