Logo

Programming-Idioms

History of Idiom 49 > diff from v29 to v30

Edit summary for version 30 by Dodopod:
New C implementation by user [Dodopod]

Version 29

2016-11-15, 22:49:43

Version 30

2017-06-05, 15:31:06

Idiom #49 Split a space-separated string

Build list chunks consisting in substrings of input string s, separated by one or more space characters.

Illustration

Idiom #49 Split a space-separated string

Build list chunks consisting in substrings of input string s, separated by one or more space characters.

Illustration
Imports
#include <string.h>
Code
chunks[0] = strtok(s, " ");
for (int i = 1; i < N; ++i)
{
    chunks[i] = strtok(NULL, " ");
    
    if (!chunks[i])
        break;
}
Comments bubble
N is the size of chunks.

strtok modifies s by adding null-characters between words.