Use the strtok function in C to split strings!

Source: Internet
Author: User
Tags strtok

Function prototype: char * strtok (char * s, char * delim );

Function: splits string s by string delim and returns the split result.

Function usage:

1. The essence of the strtok function is that strtok searches for characters contained in delim in S and replaces them with null ('/0') until the entire string is searched. This statement has two meanings: (1) each time you call the strtok function, you can only obtain one unit of separation. (2) To obtain all split units, the strtok function must be called repeatedly.

2. Replace s with null in the future calls of the strtok function.

3. Apply char s [] = "…" to the variable corresponding to the parameter S (string to be split "...." Format, rather than char * s = "...." Format.

Example:

# Include <stdio. h>

# Include <string. h>

Int main (void)

{

Char Buf [] = "golden global view ";

Char * token = strtok (BUF ,"");

While (Token! = NULL)

{

Printf ("% s", token );

Token = strtok (null, "");

}

Return 0;

}

The result is:

Golden

Global

View

 

However, if we replace char Buf [] = "golden Global View" with char * Buf = "golden Global View", an error will occur, the reason is that, if the allocated size is the same, char * P and char B [] are also different,
Char * P = "ABC ";
Char B [] = "ABC ";
After the two statements are compiled, the compiler puts "ABC" in the constant area, while strtok (char * s, char * delim) the function searches for characters contained in delim in S and replaces them with null ('/0') until the entire string is searched. This statement can reflect the difference between the pointer and the array: Since the query requires P ++/B ++. To replace it with ('/0'), you must assign a value to * P/* B.
* P = '/0 ';
* B = '/0 ';
The P Pointer Points to a constant string, and * P operations are string operations, which will obviously fail to be compiled.
B is the first address of the character array. The elements in this array are 'A', 'B', 'C', and '/0'. It looks the same as the string "ABC, but not the same. It is equivalent to storing other things '1', '2', '3', and '/0' in array B, but compiling to Char B [] = "ABC "; then, the element value in array B is changed, so the operation on * B does not affect the string.

If we replace token = strtok (null, ""); with token = strtok (BUF, ""); then the while loop becomes an infinite loop, the output result is only Golden. The reason for my explanation is as follows: In the strtok function body, there is a char type pointer (assuming char * P), which is used to make P = s, used to save the starting address of S. In subsequent processing, the value of pointer P will be stored all the time (the pointer feature in C ), therefore, the reason why null is used in future strtok calls to replace S is to prevent P from being re-assigned and point to the starting address of S, so that P can point to other locations of S, until the complete string is finally split. However, if S is used to replace null, P points to the initial address of s each time strtok is called, so that only the first split string can be obtained, in the above example, while will be an infinite loop, and the output result can only be "golden ".

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.