I am trying to remove all the whitespace in a string that is made out of multiple sentences, but I would also like to keep the periods separate from each of the tokens. Would strtok() still be useful for this or is there another function I should know about?
Asked
Active
Viewed 418 times
2 Answers
3
If you want to know where the periods are, and treat them as separate tokens, then strtok()
is not the right function. It zaps the delimiters with nulls; you don't get told which delimiter it found.
You probably need to look at:
You could also look at other questions about strtok()
and its alternatives. There are many. strtok()
is a dangerous function. You can't afford to use in a function called from another function that is also using strtok()
, nor can you afford to call any other function that uses strtok()
. You should look up POSIX strtok_r()
or Microsoft's strtok_s()
; they're safe for use in library functions. You could also look up strsep()
.
You might find one of these questions useful:
- I need a mix of
strtok()
andstrtok_single()
- Need to know when no data appears between two token separators
And there are many others that could help.

Jonathan Leffler
- 730,956
- 141
- 904
- 1,278
-
1I don't know why `strtok` is considered above all else by so many people. It's non-reentrant and requires that the string be mutable. – autistic Sep 13 '15 at 03:49
-
@Seb: yes; it is puzzling that `strtok()` is the first port of call so often. It is so seldom the best way of doing the job. – Jonathan Leffler Sep 13 '15 at 03:51
-
@Seb There's an enormous amount of wrong technical advice out there, even here on Stack Overflow. "This function has the shortest name, I'll just use it!" – says everyone ever. People also don't RTFM, don't care about `strtok()` draggin' a `static` internal thread-unsafe state with it because "it worked for me". And when used in a real-life scenario, they are surprised it fails. Go figure… – The Paramagnetic Croissant Sep 13 '15 at 05:55
0
char str[] ="- This. is a s.ample string.";
char * pch;
printf ("Splitting string \"%s\" into tokens:\n",str);
pch = strtok (str," ,-"); //any other tokens you want to use here
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, " ,-");
}

Naumann
- 357
- 2
- 13
-
1
-
2How does this 'keep the periods separate from each of the tokens'? It simply zaps the periods (and commas, dashes and spaces), doesn't it? – Jonathan Leffler Sep 13 '15 at 03:26
-
Yeah, I think I may have to go with a more trivial implementation by simply going character by character through the string. I'd be interested to read any other solutions you guys come up with though. – Sebastian Nowak Sep 13 '15 at 03:34
-
Well i never used strtok a lot, but i mostly work in C++, so don't have to. But i guess for his issue, calling strtok without period is all he need, but anyways now i guess he found what he was looking for – Naumann Sep 14 '15 at 22:02