A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://en.cppreference.com/w/cpp/algorithm/../algorithm/../../cpp/../c/string/byte/strtok.html below:

strtok, strtok_s - cppreference.com

(1)

char* strtok( char* str, const char* delim );

(until C99)

char* strtok( char* restrict str, const char* restrict delim );

(since C99)

char* strtok_s( char* restrict str, rsize_t* restrict strmax,
                const char* restrict delim, char** restrict ptr );

(2) (since C11)

Tokenizes a null-terminated byte string.

1)

A sequence of calls to

strtok

breaks the string pointed to by

str

into a sequence of tokens, each of which is delimited by a character from the string pointed to by

delim

. Each call in the sequence has a

search target 

:

Each call in the sequence searches the search target for the first character that is

not

contained in the

separator string

pointed to by

delim

, the separator string can be different from call to call.

If str or delim is not a pointer to a null-terminated byte string, the behavior is undefined.

2)

Same as

(1)

, except for the following differences:

If both str points to a character array which lacks the null character and strmax points to a value which is greater than the size of that character array, the behavior is undefined.

As with all bounds-checked functions,

strtok_s

is only guaranteed to be available if

__STDC_LIB_EXT1__

is defined by the implementation and if the user defines

__STDC_WANT_LIB_EXT1__

to the integer constant

1

before including

<string.h>

.

  1. ↑ A token may still be formed in a subsequent call with a different separator string.
  2. ↑ No more tokens can be formed in subsequent calls.
[edit] Parameters str - pointer to the null-terminated byte string to tokenize delim - pointer to the null-terminated byte string identifying delimiters strmax - pointer to an object which initially holds the size of str: strtok_s stores the number of characters that remain to be examined ptr - pointer to an object of type char*, which is used by strtok_s to store its internal state [edit] Return value

1) Returns a pointer to the first character of the next token, or a null pointer if there is no token.

2) Returns a pointer to the first character of the next token, or a null pointer if there is no token or there is a runtime-constraint violation.

[edit] Note

This function is destructive: it writes the '\0' characters in the elements of the string str. In particular, a string literal cannot be used as the first argument of strtok.

Each call to strtok modifies a static variable: is not thread safe.

Unlike most other tokenizers, the delimiters in strtok can be different for each subsequent token, and can even depend on the contents of the previous tokens.

The strtok_s function differs from the POSIX strtok_r function by guarding against storing outside of the string being tokenized, and by checking runtime constraints. The Microsoft CRT strtok_s signature matches this POSIX strtok_r definition, not the C11 strtok_s.

[edit] Example
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdio.h>
#include <string.h>
 
int main(void)
{
    char input[] = "A bird came down the walk";
    printf("Parsing the input string '%s'\n", input);
    char* token = strtok(input, " ");
    while (token)
    {
        puts(token);
        token = strtok(NULL, " ");
    }
 
    printf("Contents of the input string now: '");
    for (size_t n = 0; n < sizeof input; ++n)
        input[n] ? putchar(input[n]) : fputs("\\0", stdout);
    puts("'");
 
#ifdef __STDC_LIB_EXT1__
    char str[] = "A bird came down the walk";
    rsize_t strmax = sizeof str;
    const char* delim = " ";
    char* next_token;
    printf("Parsing the input string '%s'\n", str);
    token = strtok_s(str, &strmax, delim, &next_token);
    while (token)
    {
        puts(token);
        token = strtok_s(NULL, &strmax, delim, &next_token);
    }
 
    printf("Contents of the input string now: '");
    for (size_t n = 0; n < sizeof str; ++n)
        str[n] ? putchar(str[n]) : fputs("\\0", stdout);
    puts("'");
#endif
}

Possible output:

Parsing the input string 'A bird came down the walk'
A
bird
came
down
the
walk
Contents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'
Parsing the input string 'A bird came down the walk'
A
bird
came
down
the
walk
Contents of the input string now: 'A\0bird\0came\0down\0the\0walk\0'
[edit] References
[edit] See also finds the first location of any character in one string, in another string
(function) [edit] returns the length of the maximum initial segment that consists
of only the characters not found in another byte string
(function) [edit] returns the length of the maximum initial segment that consists
of only the characters found in another byte string
(function) [edit] finds the next token in a wide string
(function) [edit]

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4