The magical function of Strtok, splitting the string

Source: Internet
Author: User
Tags strtok

Strtok Split String function, a good solution to the requirements of character segmentation, do not have to traverse the keyword after the block character

So it's much easier to find the key in the string and get what's behind it.

#include <string.h>
Plus, like a header file.

char * strtok (char * str, const char * delimiters);

Parameter meaning

STR:: The first operation when the original string, when Strtok split once succeeded, set to NULL to continue scanning the following characters know to be empty

Delimiters  ::   tag character  Xiaowan#Xiaoli symbol #


A simple example is as follows

/ * Strtok Example * /#include <stdio.h>#include <string.h>intMain () {charStr[] ="- This, a sample string.";char* PCH; printf ("Splitting string \"%s\" into tokens:\n", str); PCH = Strtok (str," ,.-");while(PCH! = NULL) {printf ("%s\n", PCH); PCH = Strtok (NULL," ,.-");//Here to success once, set to empty, continue scanning}return0;}
Edit & Run

Run results

Splitting string "- This, a sample string." into tokens:Thisasamplestring

Based on the analysis of the results

String chant. ",--" These three characters are split up

Return ValueIf A token is found, a pointer to the beginning of the token.
Otherwise, a NULL pointer.
A NULL pointeris all returned when the end of the string (i.e., a null character) was reached in the string being Scanne D.




The magical function of Strtok, splitting the string

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.