#include <stdio.h>
The key point of the program is to define a static character pointer, which is used to record the first address of the split string.
Passing in null means that the remaining string pointed to by the static pointer continues to be processed
Char *strtok (char *str, const char *delim)
{
Static Char *rembmberlaststring=null;//Note the position of the last non-delimited string character, as shown in the illustration
const char *indexdelim=delim;//make a backup of Delim
int flag=1,index=0;//Each call to the Strtok,flag tag will cause the program to record only the first non-delimiter position, and subsequent non-delimiters are no longer processed
Char *temp=null;//return value of the program
if (str==null)
{
str=rembmberlaststring;//If STR is NULL indicates that the program continues to process the last remaining string
}
for (; *str;str++)
{
Delim=indexdelim;
for (; *delim;delim++)
{
if (*str==*delim)
{
*str=null;//If you find a character that is interesting in Delim, set the character to NULL
index=1;//used to mark characters of interest that have occurred
Break
}
}
if (*str!=null&&flag==1)
{
temp=str;//only the position of the current first non-interested character is recorded
flag=0;
}
if (*str!=null&&flag==0&&index==1)
{
rembmberlaststring=str;//the second occurrence of a non-interested character (the character of interest must have occurred before)
return temp;
}
}
rembmberlaststring=str;//executes the sentence to indicate that the character of interest has not been present, or that there is no longer a non-interest character after a character that is of interest
return temp;
}
int main (void)
{
Char strtocken[] = "A string\tof, Tokens\nand some more tokens";
Char strdelimiter[] = ", \t\n";
char * p;
p = Strtocken;
while (P=strtok (P,strdelimiter))
{
printf ("%s\n", p);
p = NULL;
}
return 0;
}
The ability to express is not very good, the technology is limited, but the general meaning is these, hope light spray
This article is from the Web Learning Summary blog, so be sure to keep this source http://8947509.blog.51cto.com/8937509/1549054
Strtok use method and principle in C language, and function of self-realization function