Strtok and strtok_r

Source: Internet
Author: User

From http://blog.csdn.net/libuding/article/details/5870089

 

Prototype: char * strtok (char * s, char * delim );

Function: Splits a string into a group of strings. S is the string to be decomposed, and delim is the separator string.

Note: during the first call, s points to the string to be decomposed, and then calls it again to set S to null.
Strtok searches for characters contained in delim in S and replaces them with null ('/0') until the entire string is searched.

Return Value: 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.

Example:

# 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", 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", P );

Return 0;
}

Two parameters must be set for the first function call. The result of the first split returns the first ',' string in the string, that is, the first time the above program outputs ABC.

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.

The functions with _ r mainly come from UNIX. The difference between all functions with and without _ r is that functions with and without _ r are thread-safe, and R means reentrant and reentrant.

 

Strtok Introduction
As we all know, strtok can be based on user-provided delimiters (and separators can also be plural numbers, such as ",").
Splits a string until it encounters "/0 ".

For example, separator = "," string = "Fred, John, Ann"
Using strtok, we can extract the three strings "Fred", "John", and "Ann.
The above C code is

Quote: int in = 0;
Char buffer [] = "Fred, John, Ann"
Char * P [3];
Char * Buf = buffer;
While (P [in] = strtok (BUF ,","))! = NULL ){
In ++;
Buf = NULL ;}

As shown in the above Code, the first execution of strtok needs to take the address of the target string as the first parameter (BUF = buffer), and then strtok needs to take null as the first parameter (BUF = NULL ). The pointer column P [] stores the split result. P [0] = "John", P [1] = "John ", P [2] = "Ann", and Buf becomes Fred/0 John/0ann/0.

 

Strtok is a thread-insecure function because it uses static allocated space to store the split string location.

Strtok and strtok_r

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.