-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,492
  • 9
  • 45
  • 75

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
  • 862
  • 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
  • 13,704
  • 3
  • 31
  • 51