bardulia
bardulia

Reputation: 171

Tokenizing a raw string in c

I would like to tokenize a string, but in a very special way.

I have the following string, formed by 3 groups of words, separated by a space:

string = abc def ghi

The thing is that I would like to load into another string all the content of string variable until the second space. That is, I would like to get:

result = abc def

And not only abc (that solution was in other forums). Please, note that the length of each word could differ.

How would I do that?

Upvotes: 0

Views: 133

Answers (1)

cnicutar
cnicutar

Reputation: 182649

I would like to load in one string all the content of string variable until the second space

How about:

char *space = strchr(string, ' ');
if (!space)
    error;
space++;
space = strchr(space, ' ');
if (!space)
    error;

Or if you know there will always be exactly 3 words, do a single strrchr (reverse). Or maybe do 2 sscanfs and then join the strings, or 2 strtoks etc.

Upvotes: 1

Related Questions