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
6
votes
5 answers

C's strtok() and read only string literals

char *strtok(char *s1, const char *s2) repeated calls to this function break string s1 into "tokens"--that is the string is broken into substrings, each terminating with a '\0', where the '\0' replaces any characters contained in string s2.…
Gilbert
  • 311
  • 1
  • 4
  • 7
6
votes
4 answers

Why does the compiler still warn me about unsafe strtok even after I define _CRT_SECURE_NO_WARNINGS?

I am using Visual Studio Express 2012 for Windows Desktop. I always get error Error C4996: 'strtok': This function or variable may be unsafe. Consider using strtok_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help…
user1870594
6
votes
3 answers

C: STRTOK exception

for some reason i get an exception at the first use of strtok() what i am trying to accomplish is a function that simply checks if a substring repeats itself inside a string. but so far i havent gotten strtok to work int CheckDoubleInput(char*…
Felix Kreuk
  • 193
  • 1
  • 9
6
votes
5 answers

C strtok() split string into tokens but keep old data unaltered

I have the following code: #include #include int main (void) { char str[] = "John|Doe|Melbourne|6270|AU"; char fname[32], lname[32], city[32], zip[32], country[32]; char *oldstr = str; strcpy(fname,…
bsteo
  • 1,738
  • 6
  • 34
  • 60
6
votes
4 answers

strtok and memory leaks

I wrote a simple url parser using strtok(). here's the code #include #include typedef struct { char *protocol; char *host; int port; char *path; } aUrl; void parse_url(char *url, aUrl *ret) { …
guigouz
  • 1,211
  • 1
  • 13
  • 18
6
votes
2 answers

Strtok(), no token match

I was trying to parse strings using strtok(); I am trying to parse strings delimited by a semicolon ( ; ). But when I input a string with no semicolons to strtok(), it returns the entire string. Shouldn't it be returning NULL if there are no token…
mrameshk
  • 79
  • 1
  • 4
6
votes
2 answers

c strtok returns NULL after return from recursion

When i'm not calling the same function in my code everything works well but when the function returns from a recursion suddenly the variable pch is NULL: void someFunction() { char * pch; char tempDependencies[100*64+100]; …
Tom
  • 9,275
  • 25
  • 89
  • 147
6
votes
3 answers

How to reversely strtok a C++ string from tail to head?

I think I need a reverse version of strtok, like: char* p = rstrtok(str, delimeters); For example, sequentially get the position of '-', '_' and '+' in the string "hello+stack_over-flow" using a delimeter set of "+_-" I only care about the…
Lyn
  • 699
  • 1
  • 7
  • 17
5
votes
1 answer

Tokenizing multiple strings simultaneously

Say I have three c-style strings, char buf_1[1024], char buf_2[1024], and char buf_3[1024]. I want to tokenize them, and do things with the first token from all three, then do the same with the second token from all three, etc. Obviously, I could…
Andy Shulman
  • 1,895
  • 3
  • 23
  • 32
5
votes
1 answer

Is my usage of fgets() and strtok() incorrect for parsing a multi-line input?

I'm writing an implementation of the Moore Voting algorithm for finding the majority element (i.e. the element which occurs more than size/2 times) in an array. The code should return the majority element if it exists or else it should return -1.…
user10648668
5
votes
3 answers

C Delete last character in string

I want to delete last character in string first, i use strtok function My Input is : "Hello World Yaho" I use " " as my delimeter My expectation is this Hell Worl Yah But the actual output is this Hello Worl Yaho How can I solve this problem? I…
JeongHyun
  • 53
  • 1
  • 1
  • 3
5
votes
1 answer

strtok when process two strings at same time

New in C and pretty confused about how to deal with several strings at the same time using strtok, for a simply example, I want to use strtok to extract the number and compare then. #include #include int main() { char…
ilyak
  • 51
  • 1
  • 2
5
votes
3 answers

Problem with using getline and strtok together in a program

In the below program , I intend to read each line in a file into a string , break down the string and display the individual words.The problem I am facing is , the program now outputs only the first line in the file. I do not understand why this is…
Eternal Learner
  • 3,800
  • 13
  • 48
  • 78
5
votes
1 answer

Getting incorrect values when accessing variables passed along in a pointer to a character array for strtok

Here is my code //Split up the config by lines int x; int numberOfConfigLines = 0; for (x = 0; x < strlen(buffer); x++) { if (buffer[x] == '\n') { numberOfConfigLines++; } } char…
Adam Nygate
  • 325
  • 3
  • 14
5
votes
3 answers

strtok wont accept: char *str

strtok wont work correctly when using char *str as the first parameter (not the delimiters string). Does it have something to do with the area that allocates strings in that notation? (which as far as i know, is a read-only area). thanks in…
bks
  • 1,886
  • 1
  • 24
  • 43