String splitting functions in C language

Source: Internet
Author: User
Tags strtok

Char *strtok (char *s, const char *delim);

Decomposes a string into a set of strings. S is the string to be decomposed, Delim is the delimiter string.

A segmented string starting from the beginning of S. Null is returned when there is no split string.

Note: When the Strtok function is called for the first time, the function ignores the spacing delimiter and returns the first character found in the Strtoken string

Number, and the symbol ends with a null character. By invoking a series of strtok functions, more symbols will be divided from the Strtoken string

Away. Each time the Strtok function is called, the Strtoken string is modified by inserting a null character after the symbol that is found. To

Reads the next symbol in the Strtoken, and when the Strtok function is called, the Strtoken parameter is null, which throws the Strtok function in the modified

The Strtoken string to find the next symbol.

#include <stdio.h>#include<string.h>intMain () {CharStr[] ="MV A.C B.C"; Char*p; P= Strtok (str," ");  while(p) {printf ("%s\n", p); P= Strtok (NULL," "); }  return 0;}

In the later version of Linux2.6.29, Strtok was replaced by STRSEP.

Specific examples are as follows:

#include <stdio.h>#include<string.h>intMain () {CharStr[] ="MV A.C B.C"; Char*p; Char*Buff; Buff=str; P= Strsep (&buff," ");  while(p!=null) {printf ("%s\n", p); P= Strsep (&buff," "); }  return 0;}

String splitting functions in C language

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.