How can I split a string into an array of strings based on a dividing character? For instance, how could I split a sentence into an array of words, separated by the space character?
Asked
Active
Viewed 368 times
-3
-
http://stackoverflow.com/questions/236129/how-to-split-a-string-in-c – L7ColWinters Feb 04 '12 at 04:26
-
possible duplicate of [how to split string to tokens in C](http://stackoverflow.com/questions/2091815/how-to-split-string-to-tokens-in-c) – Bo Persson Feb 04 '12 at 20:51
2 Answers
0
result = strtok( str, delims );
while( result != NULL ) {
printf( "result is \"%s\"\n", result );
result = strtok( NULL, delims );
}
Set delims as your delimiter

WordsWorth
- 872
- 1
- 11
- 23
0
You should use either strtok
or strtok_r
, both of which are described here (with examples). I'd recommended to use strtok_r
, since strtok
is thread-unsafe, and you may add threads to your application in the future.

Adam Mihalcin
- 14,242
- 4
- 36
- 52