Heya i ran into a little problem, well more of a mental dilemma using strtok(), i already know how this function works, heres an example code:

void split_me(char *input) {
  
  char *split = NULL; //reset split

  size_t len = strlen(input);
  char to_array[len];
  
  int i = 0;

  split = strtok(input, " \t\n"); //splits input on space, enter and tab
  
  for(i=0; split != NULL; i++) { //possible with a while loop
    to_array = strdup(split); //strdup allocates memory and fills the array

    split = strtok(NULL, " \t\n"); //to make the loop move on
  }
}

input = o joy
program output will be: ojoy

I put on some comments so that other beginners in C like me can get usefullness out of this post if they are stuck on the same problem :)

I dont guarantee this code will work, cause it is something i wrote on the fly as an example.

As you guys can see, the input is split, i wrote a simple method to keep it short.
Here is where my question arises, is it possible to make strtok() ignore certain delimeter, like not splitting the input that is within them.
For example:

input = her "my name is meli"
Desired output: her"my name is meli"

As you can see the sentence within the " " sign is not split, and that is what i have been beating my head over to achieve, looked through different functions online (google is your friend my ass).

Is it possible to do something like this with strtok()? Or do i have to write a new splitter using isspace() and use the other issomething() functions instead (crawl to the cross method), or is something like this not possible to do at all (kill me now method)?

When i was writing this i was thinking of a command prompt, more likely a shell, where u want the first argument to be a command, and then execute the command, but that will be with double array instead of a single array i would imagine.

Help appreciated thanks upfront :)

Meli :)

Recommended Answers

All 2 Replies

Is it possible to do something like this with strtok()?

No, strtok() doesn't support rules for embedded delimiters. What you want is more along the lines of a CSV parsing algorithm

Bleh il dig up some man pages on that then, thanks for the help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.