Date: 2010mar15
Updated: 2022nov15
Language: C/C++
Keywords: strtok, strtok_s_l, Microsoft Visual C++
Q. How do I use strtok_r() ?
A. Here's an example. This function uses strtok_r() to break up a string
they is delimited by semi-colons and commas.
// Linux
void BreakOnPunct(char *s)
{
const char * delim = ";,";
char * save;
char * p;
for (p = strtok_r(s, delim, &save); p; p = strtok_r(NULL, delim, &save))
{
printf("chunk=%s\n", p);
}
}
In older MSVC++ there is strtok_s() which is used just the same:
// Windows
void BreakOnPunct(LPSTR s)
{
LPCSTR delim = ";,";
LPSTR save;
LPSTR p;
for (p = strtok_s(s, delim, &save); p; p = strtok_s(NULL, delim, &save))
{
printf("chunk=%s\n", p);
}
}