hari
hari

Reputation: 9733

strtok_r to extract string inside quotes

my string is:

He is a "funny" guy

How can I extract that using strtok_r?

strtok_r(str, "\"", &last_pointer);

Is this a correct way of doing it? will the statement above skip first " ?

Upvotes: 1

Views: 423

Answers (2)

pmg
pmg

Reputation: 108978

My documentation for strtok_r says

char *strtok_r(char *str, const char *delim, char **saveptr);

On the first call to strtok_r(), str should point to the string to be parsed, and the value of saveptr is ignored. In subsequent calls, str should be NULL, and saveptr should be unchanged since the previous call.

So you should call it first with

strtok_r(str, "\"", &last_pointer);

and afterwards with

strtok_r(NULL, "\"", &last_pointer);

Upvotes: 1

user411313
user411313

Reputation: 3990

this POSIX function will skip all leading '\"' not the first. call strtok_r a second time with NULL as first parameter and have fun.

Upvotes: 1

Related Questions