I'm trying to develop a small console program that spits a string into separate words (i.e. tokens). Most of the program is working but I'm having a problem with the strtok() function. I have reviewed the code shown at How does strtok() split the string into tokens in C? and have based my own code somewhat on that. My problem lies in my makeWordList() function which is show below.
// Assumes a multi-word string was entered.
void makeWordList(char inString[], unsigned int wordCount, char * wordList[]) // Separate out words.
{
unsigned int index = 0;
const char * delimiter = " ";
char * word;
word = strtok(inString, delimiter); // Get first word.
while ((word != NULL) && (index < wordCount))
{
wordList[index++] = word; // Add word to list.
word = strtok(inString, delimiter); // Get next word.
} // end while()
}
In my case the strtok() function appears not to move along the (source) input string inString. When the program is run it produces the following output
./ex8_4
Enter string to be processed:
three word string
You entered |three word string|
There are 3 words in the string.
There are 15 letters in the string.
The word list is as follows.
three
three
three
From the output shown above it can readily be seen that strtok() successfully reads the first token (and terminates it with a "\000", according to codeblocks Watches panel) but not the subsequent tokens within inString. As I am using strtok() in a manner very similar to that shown in the code on the page linked to above, could someone please explain what it is that I am failing to understand ?