#include #include #include #define TOKEN_SPLIT ',' char * mystrtok(char * string, char token) { static char * lastp; if (string!=NULL) { lastp=string; } else { if (lastp==NULL) return NULL; lastp++; } char *holdp=lastp; char *nptr = lastp; while (*nptr!=token && *nptr!='\0') { nptr++; } if (*nptr=='\0') { lastp=NULL; } else { lastp=nptr; *nptr='\0'; } return holdp; } int main(void) { char string[50] ="Test,string1,Test,string2:Test:string3"; char *p; printf ("String \"%s\" is split into tokens using a single char in \"%c\":\n",string, TOKEN_SPLIT); p = mystrtok (string,TOKEN_SPLIT); // get first token printf ("%s\n",p); while (NULL != (p = mystrtok (NULL, TOKEN_SPLIT))) // get subsequent tokens -- NOTE USE OF NULL -- cannot split two string at same time { printf ("%s\n",p); } }