| 1 |
/* Example of strtok adapted from: |
| 2 |
https://en.cppreference.com/w/c/string/byte/strtok |
| 3 |
which is |
| 4 |
"licensed under Creative Commons Attribution-Sharealike 3.0 |
| 5 |
Unported License (CC-BY-SA) and by the GNU Free Documentation License |
| 6 |
(GFDL) (unversioned, with no invariant sections, front-cover texts, or |
| 7 |
back-cover texts). That means that you can use this site in almost any way |
| 8 |
you like, including mirroring, copying, translating, etc. All we would ask |
| 9 |
is to provide link back to cppreference.com so that people know where to |
| 10 |
get the most up-to-date content. In addition to that, any modified content |
| 11 |
should be released under an equivalent license so that everyone could |
| 12 |
benefit from the modified versions. " */ |
| 13 |
|
| 14 |
/* { dg-additional-options " -Wno-analyzer-too-complex -Wno-analyzer-symbol-too-complex" } */ |
| 15 |
|
| 16 |
#define __STDC_WANT_LIB_EXT1__ 0 |
| 17 |
#include <string.h> |
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
int main(void) |
| 21 |
{ |
| 22 |
char input[] = "A bird came down the walk"; |
| 23 |
printf("Parsing the input string '%s'\n", input); |
| 24 |
char *token = strtok(input, " "); |
| 25 |
while(token) { |
| 26 |
puts(token); |
| 27 |
token = strtok(NULL, " "); |
| 28 |
} |
| 29 |
|
| 30 |
printf("Contents of the input string now: '"); |
| 31 |
for(size_t n = 0; n < sizeof input; ++n) |
| 32 |
input[n] ? putchar(input[n]) : fputs("\\0", stdout); |
| 33 |
puts("'"); |
| 34 |
|
| 35 |
#ifdef __STDC_LIB_EXT1__ |
| 36 |
char str[] = "A bird came down the walk"; |
| 37 |
rsize_t strmax = sizeof str; |
| 38 |
const char *delim = " "; |
| 39 |
char *next_token; |
| 40 |
printf("Parsing the input string '%s'\n", str); |
| 41 |
token = strtok_s(str, &strmax, delim, &next_token); |
| 42 |
while(token) { |
| 43 |
puts(token); |
| 44 |
token = strtok_s(NULL, &strmax, delim, &next_token); |
| 45 |
} |
| 46 |
|
| 47 |
printf("Contents of the input string now: '"); |
| 48 |
for(size_t n = 0; n < sizeof str; ++n) |
| 49 |
str[n] ? putchar(str[n]) : fputs("\\0", stdout); |
| 50 |
puts("'"); |
| 51 |
#endif |
| 52 |
} |