0

I have a char array called temps that is passed to a function. The format will always be like this:

1 1.1

I want to split it up and save these two numbers. These is a space between them but after researching strtok(), I have no idea how it works.

void seperate(char *tempformat){
    char s1[10];
    char s2[10];

    s1 = strtok();
    s2 = strtok();
}
Shawn Mehan
  • 4,513
  • 9
  • 31
  • 51
Unfitacorn
  • 186
  • 2
  • 12
  • Possible duplicate of [Using strtok in c](http://stackoverflow.com/questions/8106765/using-strtok-in-c) – rkachach Nov 29 '15 at 18:35
  • `strtok` is dangerous and you'll probably want to avoid its use. You might prefer `strchr` or similar to do a nested-safe code. `strtok` has a static variable inside itself that is not changed while its called with NULL as parameter. Example of wrong behaviour: You have a function, called `pre_separate`, that needs to call your `separate` function. But `pre_separate` also uses strtok with a loop, and `separate` is also called on this loop. Well, both functions will call `strtok` with a NULL argument, messing initial pointers. – emi Nov 29 '15 at 18:46

1 Answers1

0

You can do it as :

char *result;
char s=" "; //space(delimiter)
char str[];
result = strtok(tempformat, s); 
void seperate(char *tempformat){
   while( result != NULL ) {
      printf( " %s\n", result ); //You can save the result into a array of characters as well.
      result = strtok(NULL, s);
   }
}
Naman
  • 27,789
  • 26
  • 218
  • 353