Split string code

Source: Internet
Author: User
Tags strtok

// Crt_strtok.c // compile with:/W3 // in this program, a loop uses strtok // to print all the tokens (separated by commas // or blanks) in the string named "string ". // # include <string. h> # include <stdio. h> char string [] = "a string \ TOF, tokens \ NAND some more tokens"; char SEPs [] = ", \ t \ n"; char * token; int main (void) {printf ("tokens: \ n"); // establish string and get the first token: token = strtok (String, SEPs); // c4996 // Note: strtok is deprecated; Consider using strtok_s instead while (Token! = NULL) {// while there are tokens in "string" printf ("% s \ n", token); // get next token: token = strtok (null, SEPs); // c4996} astringoftokensandsomemoretokensstrtok prototype char * strtok (char * s, const char * delim); Function Decomposition string is a set of strings. S is the string to be decomposed, and delim is the separator string. It indicates that s points to the string to be decomposed during the first call, and then calls it again to set S to null. Strtok searches for characters contained in delim in S and replaces them with null ('') until the entire string is searched. Char * P = strtok (S, ";"); P = strtok (null, ";"); During the call, the string S is changed, note This. Returns the split strings starting with S. If no split string exists, null is returned. All delim characters are filtered out, And the filtered characters are set as a separate node. The use of strtok functions in C and C ++ will destroy the integrity of the decomposed string. The s before and after the call is different. To keep the original string intact, you can use strchr and sscanf combinations. C # include <string. h> # include <stdio. h> int main (void) {char input [16] = "ABC, D"; char * P; /** // * strtok places a null terminator in front of the token, if found */P = strtok (input, ","); If (P) printf ("% s \ n", P ); /** // * a second call to strtok using a null as the first parameter returns a pointer to the character following the token */P = strtok (null ,","); if (p) printf ("% s \ n", P); Return 0;} C ++ # include <iostream> # include <cstring> using namespace STD; int main () {char sentence [] = "this is a sentence with 7 tokens "; cout <"the string to be tokenized is: \ n" <sentence <"\ n \ nthe tokens are: \ n "; char * tokenptr = strtok (sentence, ""); While (tokenptr! = NULL) {cout <tokenptr <'\ n'; tokenptr = strtok (null, "") ;}cout <"after strtok, sentence = "<sentence <Endl; return 0;} two parameters must be set for the first call of the function. Returns the first ',' string in the string, that is, the preceding string.Program The first time ABC is output. The second call to this function strtok (null, ","). The first parameter is set to null. The result returns the Split Based on the subsequent string, that is, the second Output D. Strtok is a thread-unsafe function because it uses static allocated space to store split string locations. The thread-safe function is called strtok_r, when ca uses strtok to determine the IP address or MAC address, it is necessary to use other methods to determine '. 'or': ', because it is truncated by strtok, for example: "192 .. 168. 0... 8... "This string, strtok will only be intercepted four times, in the middle... it will be treated as a key no matter how much

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.