Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement strtok and string Segmentation function with C language

2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use C language to achieve strtok and string segmentation function", the content is easy to understand, clear, hope to help you solve your doubts, the following let Xiaobian lead you to study and learn "how to use C language to achieve strtok and string segmentation function" this article.

1. Basic use of strtok

Parsing: the function roughly says the following points

1.str is the string we are going to split. Note that we will change the string, so usually we will make a copy and then split the copy of the string!

2.delimiters is our definition of syncopation symbols. If we want to use spaces as delimiters, we can define char delimiters [NUM] = "" * * Please note that there is a space in it!

Here the NUM can define its own size, and here the delimiters can be divided in a variety of ways.

The first argument of the 3.strtok function is not NULL, and the function will find the first tag in str. The strtok function will have a setting to save the address of the next location he has modified, such as "wo ai ni". If you use a space as a separator, it will replace the space with * "\ n" * *, return the position of w, and save the location of an inside the function.

The first argument to the 4.strtok function is NULL, and the function starts at the location where it was saved in the same string (that is, the location w mentioned above) and looks for the next tag.

5. If no more tags exist in the string, the NULL pointer is returned.

After the analysis, we look at a piece of code and make a cut of a string.

Int main () {char a [] = "wo shi bo zhu"; char* retArr [10]; retArr [0] = strtok (a, "); int I = 1; while (retArr [iTunes +] = strtok (NULL,")); for (int I = 0; I < 10; iTunes +) {printf ("% s", retArr [I]) } return 0;}

Second, the realization of strtok

Analysis: the situation coming in is roughly divided into two cases, str is empty and not empty. If the function is called for the first time, because we want to record the position of the next time we come in, we choose to create a static variable of static inside the function, static char* p_last = NULL, and the first time we set it to NULL, the function is divided into two cases. If we cut, we use the tmp pointer to go back, and the str pointer saves the current position. So when the tmp pointer goes to\ 0, we have to process the p_last as NULL. In other cases (the str string can also be separated by delimiters), we will put * tmp ='\ 0pointer inside the function, and then process the p_last as the position of the next entry.

Example:

Int main () {char a [] = "wo ai ni"; char* retArr [10]; retArr [0] = my_strtok (a, "); int I = 1; while (retArr [iTunes +] = my_strtok (NULL,")); for (int I = 0; I < 10; iTunes +) {printf ("% s", retArr [I]) } return 0;}

First segmentation

Second segmentation

The third segmentation

The fourth cut

The implementation code is as follows, and you need to fetch it yourself:

When char* my_strtok (char* str, char* delimiters) {/ / str sends NULL, if there is a record last time, use the next position of the previous time, otherwise it will be NULL static char* p_last = NULL; if (str = = NULL & & p_last = = NULL) return NULL If (str = = NULL) {/ / is used to return the current position str = pendant; char* tmp = pairast; int len = strlen (delimiters); while (* tmp) {for (int I = 0; I < len) + + I) {if (* tmp = = delimiters [I]) {p_last = tmp+1; * tmp ='\ 0' Return str;}} / / coming here means that the current tmp does not have a tmp++ that can be found in delimiters } / / when you come to the slash\ 0 position, you should set p_last to NULL char* ret = pendant; p_last = NULL; return ret;} else {char* tmp = str. Int len = strlen (delimiters); while (* tmp) {for (int I = 0; I < len) + + I) {if (* tmp = = delimiters [I]) {p_last = tmp+1; * tmp ='\ 0' Return str;}} / / coming here means that the current tmp does not have a tmp++ that can be found in delimiters } / / found, can not find char* ret = pairast; p_last = NULL; return ret;}} above is "how to use C language to implement strtok and string segmentation function" all the content of this article, thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report