Lines 12 - 19. Having a NESTED loop with strtok as an inner while loop defeats the whole purpose of strtok, particularly the way you have it. Suppose you have three words in the string. For i equals 0, the inner loop from lines 14 to 18 will execute 3 times. i will equal 0 all three times, so you'll copy a string to the same location three times, effectively overwriting twice. p then becomes NULL and stays NULL, so the inner loop will never execute for i greater than 0. How can it?
So one, why are you using a loop within a loop at all? Two, this particular nested loop makes no sense because it's really THIS...
for (int i = 0; i < 1; i++)
{
while (p != NULL)
{
strcpy ( strWords[i] , p);
p = strtok (NULL, ".,? ;");
}
}
which means it's this...
while (p != NULL)
{
strcpy ( strWords[0] , p);
p = strtok (NULL, ".,? ;");
}
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
p = strtok (p, " "); // wrong
You had it right the first time, or at least the first parameter. NULL, not p.
p = strtok (NULL, ".,? ;"); // right
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
As the loop starts on line 2, what is the value of p and count?
Where does the value of count come from?
Have you called strtok() and assigned the return value to p before the loop on line 2 starts?
Lerner
Nearly a Posting Maven
2,382 posts since Jul 2005
Reputation Points: 739
Solved Threads: 396
for (char *p = strtok(str, fmt); count < MAX && p; p = strtok(0, fmt))
{
strcpy(strWords[count++], p);
}
Line 3 turns into this...
strcpy(strWords[count], p);
count++;
so we have...
for (char *p = strtok(str, fmt); count < MAX && p; p = strtok(0, fmt))
{
strcpy(strWords[count], p);
count++;
}
[/code]
char *p = strtok(str, fmt)
[/code]
Finds the first token in str and make p point to it.
count < MAX && p
is the same as...
count < MAX && p != NULL
Means "keep going until either count >= MAX or p is NULL". If p is NULL, then all tokens have been found... http://www.cplusplus.com/reference/clibrary/cstring/strtok/
Return Value
A pointer to the last token found in string.
A null pointer is returned if there are no tokens left to retrieve.
p = strtok(0, fmt)
is the same as...
p = strtok(NULL, fmt)
means "move forward till you find the next token and make p point to it."Parameters
str
C string to truncate. The contents of this string are modified and broken into smaller strings (tokens).
Alternativelly, a null pointer may be specified, in which case the function continues scanning where a previous successful call to the function ended.
delimiters
C string containing the delimiters.
These may vary from one call to another.
See the link. They have a good explanation and an example.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
Can't seem to edit post. I screwed up a code tag.
VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711