Strtok written by yourself

Source: Internet
Author: User
Tags strtok

# Include "stdio. H "</P> <p> char * strtok (char * s, char * delim) <br/>{< br/> static char * Last = s; // Save the remaining string <br/> char * Start = NULL; // The start address of the string returned each time <br/> char * STR = NULL; </P> <p> If (last = NULL) <br/>{< br/> return NULL; <br/>}</P> <p> If (S = NULL) <br/> Start = last; <br/> else <br/> Start = s; // when the string is not empty </P> <p> STR = start; <br/> char * CP; <br/> while (* Str) <br/>{< br/> CP = delim; <br/> while (* CP) <br/>{< br/> If (* STR = * CP) <br/>{< br/> * STR = '/0'; <br/> last = STR + 1; <br/> return start; <br/>}< br/> CP ++; <br/>}< br/> STR ++; <br/>}< br/> If (* STR = '/0') <br/>{< br/> last = NULL; <br/> return start; <br/>}< br/> return NULL; </P> <p >}< br/> void main () <br/>{</P> <p> char STR [] = {"test: Is: A, string: toking "}; </P> <p> char * str1 = strtok (STR, ":,"); <br/> int x = 1; </P> <p> printf ("% d: % s/n", X, str1); </P> <p> while (1) <br/>{< br/> str1 = strtok (null, ":,"); <br/> If (str1 = NULL) <br/>{< br/> printf ("Finished/N"); <br/> break; <br/>}< br/> X ++; <br/> printf ("% d: % s/n", X, str1); </P> <p >}< br/>}

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.