This end of the token is automatically replaced by a null-character by the function
Therefore you cannot call strtok several times on the same string. You could access each token with code like this:
1 2 3 4 5
constchar* ptr = str;
for(int i = 0; i < wordCount; i++){
cout << ptr;
ptr += strlen(ptr)+1;
}
I'll explain. In the beginning your string was, for example, "Hello, world0". (0 here represents the terminating null character. After multiple calls to strtok the string is "Hello0 world0" so basically there are two strings now. If you have a pointer which points to 'H', to move it to the next word you have to add the length of "Hello0" which is strlen(ptr)+1. +1 because strlen doesn't count the null character.
while (p != NULL)
{
/* p is not null; it points to a token;
if you don't store it somewhere, the next line will make you lose it */
p = strtok (NULL, " ,.-");
wordCount++;
}
either
-tokenize the string like you do
-allocate an array
-use my code, just replace the cout with copying the string.
or
-use std::vector with filipe's code. you may want to use std::string too.
vector is a sort of dynamic array. just push the strings onto it. (google how vectors work)