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

Arduino Error: cannot convert 'String' to 'char*' for argument '1' to 'char* strtok(char*, const char*)'

I'm working on an arduino assignment that splits an incoming string and puts the terms of the string in 6 different variables( a sample input string when split up has 6 terms). i have the following error popping up: cannot convert 'String' to…
3
votes
2 answers

strtok: How to store tokens in two different buffers

I have a string containing datatypes and addresses of variables. These values are separated by "///" and they are alternating (type /// address /// type /// address ...). The amount of these tuples is not fixed and can vary from execution to…
Gora
  • 61
  • 12
3
votes
2 answers

how to divide words with strtok in an array of chars in c

I have a struct named excuses that has chars, I need to store at least 20 excuses. Then, I need to divide each word of each excuse in an array. ¿How i can do that? #define excuseLength 256 typedef struct{ char…
3
votes
1 answer

string.h output words C

I need to compare the first and the last letter in a word; if these letters are the same, I need to output that word to a file. But I take words from another file. My problem is i can't guess how i should output all words because in my code, it…
Nikita Gusev
  • 143
  • 10
3
votes
2 answers

Parse $PATH variable and save the directory names into an array of strings

I want to parse the $PATH variable of Linux, and then save the directory names that are getting separated with ':' into an array of strings. I know it's a simple task but I am stuck and any help would be nice. My code so far is something like this…
Sarriman
  • 382
  • 5
  • 22
3
votes
1 answer

strtok() overwrites its source string

I'm writing a toy bash shell. My goal right now is to cycle through the environment looking for the path a specific command can be found at. Right now I am delimiting the PATH (e.g. "/home/user/bin:home/user/.local/bin:/usr/local/sbin" etc) by ":",…
teleTele
  • 33
  • 3
3
votes
1 answer

Getting last token of strtok

I'm making a program that accepts a number and then parses the a file to return the name associated with that number. It's mostly done, but there's just one last step. Right now, my program correctly finds the line associated with the given number…
Bob
  • 715
  • 2
  • 11
  • 32
3
votes
3 answers

Why is strtok printing only first word?

I am using strtok for converting string into individual words. I have done the following: int main() { char target[100]; char *t; scanf("%s",target); t = strtok(target," "); while (t!= NULL) { printf("<<%s>>\n", t); t = strtok…
Tehreem
  • 476
  • 9
  • 23
3
votes
2 answers

C programming - sscanf for tkens separated by space

I'm currently trying writing a program using C (very new to C - only been learning it for 2 weeks), and I wanted to get a string of input from the user by stdin, in which the string has a char, followed by 2 floats (each has space in between).…
Boku
  • 63
  • 9
3
votes
3 answers

Parsing mmaped file with strtok?

Here is my problem: I want to map the file "filename.txt", which basically consists of two pairs of strings per line: "string1 string2 string3 string4 string5 string6..." and then I wanted to separate the different strings using strtok. So I map…
Inês
  • 587
  • 1
  • 4
  • 10
3
votes
1 answer

Parsing memory mapped file C

I'm currently building a functional DNS Server and I need some help to finish it. Currently I turn the server on and with the dig command I'm able to send my requests. The problem is how to answer them correctly. Before i have mapped in memory a…
Pedro Caseiro
  • 161
  • 1
  • 16
3
votes
2 answers

C - split a string with pipes using strtok

I have a string "1|4|1577|1|10.22.33|7001390280000019|||||172.20.5.20|1" and I want to split this string to get a result like this: 1 4 1577 10.22.33 7001390280000019 null null null null 172.20.5.20 1 But when I use strtok in a while cycle, the…
Alan Gaytan
  • 852
  • 4
  • 14
  • 33
3
votes
1 answer

C++ Tokenizer Complexity vs strtok_r

I'm making this question because I moved my tokenizer from strtok_r to an equivalent version in C++. I have to use strtok_r in place of strtok, because I have 2 nested tokenizations to perform most of the time. The strtok_r algorithm is something…
nicolati
  • 31
  • 3
3
votes
2 answers

Get only one token from strtok at a time

Consider the following scenario with two different strings: Row1: MyID-MyName-MyAddress-MyNumber-MyNumber2-MyAlias Row2: MyID-MyName-MyAddress-MyNumber--MyAlias In the second example, the value for MyNumber2 is missing. I need to extract each…
Mikel Urkia
  • 2,087
  • 1
  • 23
  • 40
3
votes
2 answers

strtok not working as expected, works only for first few iterations

I'm using strtok to split up a string, it works as expected for the first 4 iterations, but starts messing up after that. The program is supposed to take a line such as "david 1 2 3 4 5" and print out the name plus the sum of the numbers "david…
Vaderico
  • 629
  • 2
  • 8
  • 24