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
23
votes
3 answers

How to use Strtok for tokenizing a Const char*?

I have a const char* variable which may have a value like "OpenStack:OpenStack1". I want to tokenize this const char* using strtok where the delimiter(which is of a const char* type) is ":" . But the problem is strtok is of following type: char *…
the_naive
  • 2,936
  • 6
  • 39
  • 68
20
votes
2 answers

strtok function thread safety

I have been spending some time in debugging a programme which gives segmentation fault. The bug is quite indeterministic and intermittent, which is annoying. I narrowed it down to the calling of strtok(). I suspect that it is the calling of strtok()…
Steveng
  • 2,385
  • 6
  • 23
  • 20
19
votes
8 answers

strtok segmentation fault

I am trying to understand why the following snippet of code is giving a segmentation fault: void tokenize(char* line) { char* cmd = strtok(line," "); while (cmd != NULL) { printf ("%s\n",cmd); cmd = strtok(NULL, " "); }…
user1162954
  • 267
  • 2
  • 4
  • 7
18
votes
5 answers

Converting char * to Uppercase in C

I'm trying to convert a char * to uppercase in c, but the function toupper() doesn't work here. I'm trying to get the name of the the value of temp, the name being anything before the colon, in this case it's "Test", and then I want to capitalize…
EDEDE
  • 193
  • 1
  • 1
  • 4
18
votes
6 answers

Segmentation Fault when using strtok_r

Can anyone explain why I am getting segmentation fault in the following example? #include #include int main(void) { char *hello = "Hello World, Let me live."; char *tokens[50]; strtok_r(hello, " ,", tokens); int i = 0; …
17
votes
7 answers

Using strtok in c

I need to use strtok to read in a first and last name and seperate it. How can I store the names where I can use them idependently in two seperate char arrays? #include #include int main () { char str[] ="test string."; …
shinjuo
  • 20,498
  • 23
  • 73
  • 104
17
votes
2 answers

Nested strtok function problem in C

I have a string like this: a;b;c;d;e f;g;h;i;j 1;2;3;4;5 and i want to parse it element by element. I used nested strtok function but it just splits first line and makes null the token pointer. How can i overcome this? Here is the code: token =…
mausmust
  • 173
  • 1
  • 1
  • 4
17
votes
7 answers

C language: How to get the remaining string after using strtok() once

My string is "A,B,C,D,E" And the separator is "," How can I get the remaining string after doing strtok() once, that is "B,C,D,E" char a[] = "A,B,C,D,E"; char * separator = ","; char * b = strtok(a,separator); printf("a: %s\n", a); printf("b: %s\n",…
Eric Tseng
  • 553
  • 3
  • 8
  • 22
16
votes
2 answers

Why should strtok() be deprecated?

I hear this from a lot of programmers that the use of strtok maybe deprecated in near future. Some say it is still. Why is it a bad choice? strtok() works great in tokenizing a given string. Does it have to do anything with the time and space…
Pushan Gupta
  • 3,697
  • 5
  • 23
  • 39
15
votes
6 answers

strtok - char array versus char pointer

Possible Duplicate: strtok wont accept: char *str When using the strtok function, using a char * instead of a char [] results in a segmentation fault. This runs properly: char string[] = "hello world"; char *result = strtok(string, " "); This…
Elle H
  • 11,837
  • 7
  • 39
  • 42
14
votes
5 answers

How to use strtok in C properly so there is no memory leak?

I am somewhat confused by what happens when you call strtok on a char pointer in C. I know that it modifies the contents of the string, so if I call strtok on a variable named 'line', its content will change. Assume I follow the bellow…
user246392
  • 2,661
  • 11
  • 54
  • 96
14
votes
3 answers

Using strtok() in nested loops in C?

I am trying to use strtok() in nested loops but this is not giving me desired results, possibly because they are using the same memory location. My code is of the form:- char *token1 = strtok(Str1, "%"); while (token1 != NULL) { char *token2 =…
Alex Xander
  • 3,903
  • 14
  • 36
  • 43
13
votes
5 answers

Separating a string in C++

I am trying to separate a string into multiple strings, to make a customized terminal. So far I have been separating control signals using strtok, however I do not understand how to separate specific instances of a character. For example: string…
divyanshch
  • 390
  • 5
  • 15
13
votes
3 answers

Is there a way to count tokens in C?

I'm using strtok to split a string into tokens. Does anyone know any function which actually counts the number of tokens? I have a command string and I need to split it and pass the arguments to execve() . Thanks! Edit execve takes arguments as…
James
  • 1,430
  • 4
  • 20
  • 27
12
votes
4 answers

strtok_r for MinGW

strtok_r is the reentrant variant of strtok. It is POSIX-conformant. However, it is missing from MinGW, and I'm trying to compile a program that is using it. Is there any way I could add a standard implementation of this function, perhaps to the…
sashoalm
  • 75,001
  • 122
  • 434
  • 781
1
2
3
93 94