-3

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?

Jumhyn
  • 6,687
  • 9
  • 48
  • 76

2 Answers2

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