C-language cutting multilayer strings (Strtok_r strtok use method) _c language

Source: Internet
Author: User
Tags strtok

1. Strtok Introduction

As is known to all, Strtok can be based on the user-supplied delimiter (which can also be plural for example). ”)
Splits a section of string until it encounters a "yes".

For example, the delimiter = "," string = "Fred,john,ann"

by Strtok, 3 strings of "Fred" "John" "Ann" can be extracted.

The C code above is

Copy Code code as follows:

int in=0;
Char buffer[]= "Fred,john,ann"
Char *p[3];
char *buff = buffer;
while ((P[in]=strtok (buf, ","))!=null) {
i++;
Buf=null; }

As the code above, the first execution of the strtok requires the address of the destination string to be the first parameter (Buf=buffer), after which the strtok needs to be NULL as the first parameter (buf=null). Pointer column p[], then stores the split result, p[0]= "John", p[1]= "John", p[2]= "Ann", and Buf becomes fred\0john\0ann\0.

2. Weaknesses of the Strtok

Let's change our plan: We have a string "Fred male 25,john male 62,anna female 16" We want to sort this string into a struct,

Copy Code code as follows:

struct Person {
Char [] name;
char [6] sex;
Char [4] age;
}

One way to do this is to first extract a string that is "," and then split it into "" (space).
For example: Intercept "Fred male 25" and split it into "Fred" "Male" "25"
I've written a little program to show this process:

Copy Code code as follows:

#include <stdio.h>
#include <string.h>
#define INFO_MAX_SZ 255
int main ()
{
int in=0;
Char buffer[info_max_sz]= "Fred male 25,john male 62,anna female 16";
Char *p[20];
Char *buf=buffer;

while ((P[in]=strtok (buf, ","))!=null) {
Buf=p[in];
while ((P[in]=strtok (buf, ""))!=null) {
in++;
Buf=null;
}
p[in++]= "* * *"; Performance segmentation
Buf=null; }

printf ("Here we are have%d strings\n", in);
for (int j=0; j<in; j + +)
printf (">%s<\n", P[j]);
return 0;
}

The output of this program is:
Here we have 4 strings
>Fred<
>male<
>25<
>***<
This is just a little piece of data, not what we need. But why is that? This is because Strtok uses a static (static) pointer to manipulate the data, let me analyze the above code to run the process:

Red is the location of the strtok's built-in pointer, and the blue is strtok to the string modification

1. "Fred male 25,john male 62,anna female 16"//outer circulation

2. "Fred male 25\0john male 62,anna female 16"/into the inner loop

3. "Fred\0male 25\0john male 62,anna female 16"

4. "Fred\0male\025\0john male 62,anna female 16"

5 "Fred\0male\025\0john male 62,anna female 16"//Inner Loop Encounter "" "back to outer loop

6 "Fred\0male\025\0john male 62,anna female 16"//Outer loop encountered "" "Run end.

3. Use of Strtok_r

In this case we should use Strtok_r, Strtok reentrant.
Char *strtok_r (char *s, const char *delim, char **ptrptr);

Relative strtok we need to provide strtok with a pointer to operate, rather than using matching pointers like strtok.
Code:

Copy Code code as follows:

#include <stdio.h>
#include <string.h>
#define INFO_MAX_SZ 255
int main ()
{
int in=0;
Char buffer[info_max_sz]= "Fred male 25,john male 62,anna female 16";
Char *p[20];
Char *buf=buffer;

Char *outer_ptr=null;
Char *inner_ptr=null;

while ((P[in]=strtok_r (buf, ",", &outer_ptr))!=null) {
Buf=p[in];
while ((P[in]=strtok_r (buf, "", &inner_ptr))!=null) {
in++;
Buf=null;
}
p[in++]= "* * *";
Buf=null; }

printf ("Here we are have%d strings\n", in);
for (int j=0; j<in; j + +)
printf (">%s<\n", P[j]);
return 0;
}

This time the output is:
Here we have strings
>Fred<
>male<
>25<
>***<
>John<
>male<
>62<
>***<
>Anna<
>female<
>16<
>***<


Let me analyze the running process of the above code:

Red is the position of the Strtok_r Outer_ptr Point,
Purple is the position of the inner_ptr point of Strtok_r,
Blue for strtok modification of strings

1. "Fred male 25,john male 62,anna female 16"//outer circulation
2. "Fred male 25\0john male 62,anna female 16"/into the inner loop
3. "Fred\0male 25\0john male 62,anna female 16"
4 "Fred\0male\025\0john male 62,anna female 16"
5 "Fred\0male\025\0john male 62,anna female 16"//Inner Loop Encounter "" "back to outer loop
6 "Fred\0male\025\0john male 62\0anna female 16"//Into the inner loop

Originally, the function modifies the original string.

Therefore, when you pass in a char *test2 = "Feng,ke,wei" as the first parameter, a memory error occurs at the location ① because the contents of the Test2 point are saved in the literal constant area and the contents of the area cannot be modified. and char test1[] = "Feng,ke,wei" in the Test1 point to the content is stored in the stack area, so you can modify

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.