Turn from: http://blog.csdn.net/ast_224/article/details/3977311
Strtok
#include <string.h>
Char *strtok (char *str, const char *delim);
Char *strtok_r (char *str, const char *delim, char **saveptr);
Function: Explode a string into a set of tag strings. STR is the string to be decomposed, Delim as a delimiter string.
Note: At the first invocation, STR must point to the string to be decomposed, and then call to set S to null.
Strtok finds the characters contained in Delim in STR and replaces it with null ('/0 ') until the entire string is searched.
Returns a pointer to the next tag string. Returns the null character null when no string is marked.
Example: Use Strtok to determine whether an IP address is legitimate: ip_strtok.c:
[C-sharp] View plain copy//ip_strtok.c #include <stdio.h> #include < string.h> Int main (INT&NBSP;ARGC,&NBSP;CHAR&NBSP;**ARGV) { char temp_buf[100] = {}; char p_temp[100]; char *p=NULL; char *t = "."; int m,n,i; int j=0,s=0; if (argc!=2) { printf ("param must 2/n"); return -1; } strcpy (temp_buf, argv[1]); for (I=0; i<strlen (TEMP_BUF); i++) { if (temp_buf[i] == *t) j++; if (temp_buf[i] = = *t && (temp_buf[i+1]>= ' 0 ' &&temp_buf[i+1]<= ' 9 ')) { s++; } } if (j!=3 | | j!=s) { printf ("Ip param format error/n "); return -1; } p = strtok (temp_buf, t); while (p!=null) { strcpy (p_temp, p); printf ("%s /n", p_temp); &Nbsp; for (N=0; n<strlen (p_temp); n++) { if ( p_temp[n]>= ' 0 ' &&p_temp[n]<= ' 9 ') { printf ("ip param error/n"); return -1; } } m = atoi (p_temp); if (m>255) { printf ("ip invalid /n"); return -1; } p=strtok (null, "."); printf ("p = %s/n", p); } printf ("OK