Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/string/byte/strtok"

From cppreference.com
< cpp‎ | string‎ | byte
(added example, fixed typo)
m (r2.7.3) (Robot: Adding de, es, it, pt, ru)
Line 56: Line 56:
 
{{dcl list end}}
 
{{dcl list end}}
  
 +
[[de:cpp/string/byte/strtok]]
 +
[[es:cpp/string/byte/strtok]]
 
[[fr:cpp/string/byte/strtok]]
 
[[fr:cpp/string/byte/strtok]]
 +
[[it:cpp/string/byte/strtok]]
 
[[ja:cpp/string/byte/strtok]]
 
[[ja:cpp/string/byte/strtok]]
 +
[[pt:cpp/string/byte/strtok]]
 +
[[ru:cpp/string/byte/strtok]]
 
[[zh:cpp/string/byte/strtok]]
 
[[zh:cpp/string/byte/strtok]]

Revision as of 15:57, 2 November 2012

Template:ddcl list begin <tr class="t-dsc-header">

<td>
Defined in header <cstring>
</td>

<td></td> <td></td> </tr> <tr class="t-dcl ">

<td class="t-dcl-nopad">
char* strtok( char* str, const char* delim );
</td>

<td class="t-dcl-nopad"> </td> <td class="t-dcl-nopad"> </td> </tr> Template:ddcl list end

Finds the next token in a null-terminated byte string pointed to by str. The separator characters are identified by null-terminated byte string pointed to by delim.

If str != NULL, the function searches for the first character which is not separator. This character is the beginning of the token. Then the function searches for the first separator character. This character is the end of the token. Function terminates and returns NULL if end of str is encountered before end of the token is found. Otherwise, a pointer to end of the token is saved in a static location for subsequent invocations. This character is then replaced by a NULL-character and the function returns a pointer to the beginning of the token.

If str == NULL, the function continues from where it left in previous invocation. The behavior is the same as if the previously stored pointer is passed as str.

Contents

Parameters

str - pointer to the null-terminated byte string to tokenize
delim - pointer to the null-terminated byte string identifying delimiters

Return value

Pointer to the beginning of a token if the end of string has not been encountered. Otherwise returns NULL.

Note

The function is not thread safe.

Example

#include <cstring>
#include <iostream>
 
int main() 
{
    char input[100] = "A bird came down the walk";
    char *token = std::strtok(input, " ");
    while (token != NULL) {
        std::cout << token << '\n';
        token = std::strtok(NULL, " ");
    }
}

Output:

A
bird
came
down
the
walk

See also

C documentation for strtok