Questions tagged [strtok]

strtok() is a Standard C (ISO 9899:1989) function for splitting a string in tokens. strtok_r() is the thread-safe variant defined by IEEE Std 1003.1:2004 (aka "POSIX").

NAME

strtok, strtok_r - split string into tokens

SYNOPSIS

#include <string.h>

char *strtok(char *restrict s1, const char *restrict s2);
char *strtok_r(char *restrict s, const char *restrict sep,
       char **restrict lasts);
1404 questions
133
votes
16 answers

How does strtok() split the string into tokens in C?

Please explain to me the working of strtok() function. The manual says it breaks the string into tokens. I am unable to understand from the manual what it actually does. I added watches on str and *pch to check its working when the first while loop…
user379888
59
votes
13 answers

Using strtok with a std::string

I have a string that I would like to tokenize. But the C strtok() function requires my string to be a char*. How can I do this simply? I tried: token = strtok(str.c_str(), " "); which fails because it turns it into a const char*, not a char*
Bill
44
votes
8 answers

How to split a string to 2 strings in C

I was wondering how you could take 1 string, split it into 2 with a delimiter, such as space, and assign the 2 parts to 2 separate strings. I've tried using strtok() but to no avail.
Mark Szymanski
  • 56,590
  • 24
  • 70
  • 87
43
votes
4 answers

C: correct usage of strtok_r

How can I use strtok_r instead of strtok to do this? char *pchE = strtok(NULL, " "); Now I'm trying to use strtok_r properly... But sometimes I get problems with the strtol. I have a thread that I execute 10 times (at the same time). char…
Carlcox89
  • 583
  • 1
  • 4
  • 9
43
votes
3 answers

Split string into tokens and save them in an array

How to split a string into an tokens and then save them in an array? Specifically, I have a string "abc/qwe/jkh". I want to separate "/", and then save the tokens into an array. Output will be such that array[0] = "abc" array[1] = "qwe" array[2] =…
Syeda Amna Ahmed
  • 665
  • 2
  • 6
  • 10
36
votes
4 answers

Why is strtok() Considered Unsafe?

What feature(s) of strtok is unsafe (in terms of buffer overflow) that I need to watch out for? What's a little weird to me is that strtok_s (which is "safe") in Visual C++ has an extra "context" parameter, but it looks like it's the same in other…
user541686
  • 205,094
  • 128
  • 528
  • 886
34
votes
3 answers

What are the differences between strtok and strsep in C

Could someone explain me what differences there are between strtok() and strsep()? What are the advantages and disadvantages of them? And why would I pick one over the other one.
mizuki
  • 343
  • 1
  • 3
  • 4
29
votes
6 answers

Why do we use NULL in strtok()?

Why do we use NULL in strok() function? while (h != NULL) { h = strtok(NULL, delim); if (hold != NULL) printf("%s", hold); } What does this program do when *h is pointing to a string?
user3600999
  • 373
  • 1
  • 3
  • 8
28
votes
4 answers

How does the strtok function in C work?

I found this sample program which explains the strtok function: #include #include int main () { char str[] ="- This, a sample string."; char * pch; printf ("Splitting string \"%s\" into tokens:\n",str); pch =…
user2426316
  • 7,131
  • 20
  • 52
  • 83
26
votes
6 answers

What's the difference between strtok_r and strtok_s in C?

I'm trying to use this function in a C program that needs to be able to compile in Linux and Windows. At first I tried using strtok_r, but then when I compiled on windows, it complained about the function not existing and said it would assume it's…
petranaya
  • 769
  • 1
  • 6
  • 23
25
votes
4 answers

Whats The use of function strtok() in PHP, how is better than other string function doing the same thing?

Whats the use of function strtok() in PHP? How is better than other string function doing the same thing?
OM The Eternity
  • 15,694
  • 44
  • 120
  • 182
25
votes
4 answers

What's the difference between strtok and strtok_r in C?

What's the difference between strtok and strtok_r in C and when are we supposed to use which?
dreamer_999
  • 1,465
  • 3
  • 17
  • 22
24
votes
2 answers

strtok() - Why you have to pass the NULL pointer in order to get the next token in the string?

This is the explanation of strtok(). #include char* strtok( char* s1, const char* s2 );* The first call to strtok() returns a pointer to the first token in the string pointed to by s1. Subsequent calls to strtok() must…
phez1
  • 1,477
  • 3
  • 14
  • 19
24
votes
4 answers

Do I need to free the strtok resulting string?

Or rather, how does strtok produce the string to which it's return value points? Does it allocate memory dynamically? I am asking because I am not sure if I need to free the token in the following code: The STANDARD_INPUT variables is for exit…
Sunspawn
  • 817
  • 1
  • 12
  • 27
23
votes
3 answers

Why is strtok changing its input like this?

Ok, so I understand that strtok modifies its input argument, but in this case, it's collapsing down the input string into only the first token. Why is this happening, and what can I do to fix it? (Please note, I'm not talking about the variable…
user1209326
  • 795
  • 3
  • 8
  • 17
1
2 3
93 94