>this is a quote taken from my text book
Your textbook misspelled sentence?
strtok is fine if you understand that it modifies the original string. This means that if you need the original for anything later, you need to make a copy first, and if the original string is const in anyway then you also need to make a writable copy. A better C++ solution is:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
void split_string ( const char *s )
{
istringstream in ( s );
string word;
while ( in>> word ) {
cout<< word <<endl;
}
}
int main()
{
const char *s = "This is a test";
split_string ( s );
cout<< s <<endl;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401