The Strtok () function cannot handle the const char* directly, and a char array is obtained with strncpy, and the temp array is processed.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
Char *string = "t01:91+:123";
Char tmp[16];
char* s;
/* Strtok can does with const char*, so I use the strncpy copy it to an char array *
/strncpy (tmp, String, sizeof (string ));
s = strtok (tmp, "T");
printf ("String:%s\n", string); 01:91+:123
s = strtok (S, ":");
printf ("City ID:%s\n", s);
return 0;
}
----------
Gist:https://gist.github.com/wusuopubupt/64cd0cb29bf67ed82af4
Update:
------------
#include <string.h>
#include <stdio.h>
int main (void)
{
char input[16]= "abc,d";
char *p;
P=strtok (Input, ",");
if (p) printf ("%s\n", p);
P=strtok (NULL, ","); Attention here.
if (p) printf ("%s\n", p);
return 0;
}
Output:
Abc
D