Strtok use method and principle in C language, and function of self-realization function

Source: Internet
Author: User
Tags strtok


#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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.