首页 > 编程知识 正文

c语言中strstr函数的用法,c语言字符串分割函数

时间:2023-05-03 14:23:55 阅读:144938 作者:2834

c语言的strtok函数

In this article,we’lltakealookatusingthestrtok () and strtok_r ) ) functions in C。

本文介绍如何在c语言中使用strtok (和strtok_r )函数。

These functions are very useful,ifyouwanttotokenizeastring.cprovidesthesehandyutilityfunctionstosplitourinputstringintototokens

这些功能对于标记字符串很有用。 c提供了一个有用的实用程序函数,用于将输入字符串拆分为标签。

let’stakealookatusingthesefunctions,using suitable examples。

使用适当的例子来看看如何使用这些功能。

使用strtok (函数First,let’slookatthestrtok ) function。

首先,strtok (让我们看看函数。

thisfunctionisapartofthestring.hheaderfile,soyoumustincludeitinyourprogram。

此函数是string.h头文件的一部分,因此必须包含在程序中。

# include string.h char * strtok (char * str,const char* delim ); thistakesinaninputstringstrandadelimitercharacterdelim。

这将接受输入字符串str和分隔符delim。

strtok (willsplitthestringintotokensbasedonthedelimitedcharacter )。

strtok ) )根据分隔符将字符串拆分为标签。

weexpectalistofstringsfromstrtok ().butthefunctionreturnsusasinglestring! Why is this?

我们期待着从strtok ()得到字符串列表。 但是函数返回字符串! 为什么是这样?

thereasonishowthefunctionhandlesthetokenization.aftercallingstrtok (input,delim ),it returns the first token。

其原因是函数如何处理标记。 调用strtok(input,delim )并返回第一个标记。

butwemustkeepcallingthefunctionagainandagainonanullinputstring,until we get NULL!

但是,必须用NULL输入字符串一次又一次地调用函数。 直到得到空值!

基本,weneedtokeepcallingstrtok (空,戴尔) until it returns空值。

基本上,必须继续调用strtok(NULL,delim ),直到返回null。

Seems confusing? let’slookatanexampletoclearitout!

你好像很困惑吗? 看看一个例子就通关吧!

# include stdio.h # include string.hint main ()/ourinputstringcharinput _ string ()='Hellofromjournaldev!' ; //ouroutputtokenlistchartoken _ list [ 20 ] [ 20 ]//wecallstrtok (输入,戴尔) togetourfirsttoken//noticethedoublbltok itisstillachar * singlecharacterstring! char*token=strtok(input_string,''); int num_tokens=0;

// Index to token list. We will append to the list while (token != NULL) { // Keep getting tokens until we receive NULL from strtok() strcpy(token_list[num_tokens], token); // Copy to token list num_tokens++; token = strtok(NULL, " "); // Get the next token. Notice that input=NULL now! } // Print the list of tokens printf("Token List:n"); for (int i=0; i < num_tokens; i++) { printf("%sn", token_list[i]); } return 0;}

So, we have our input string “Hello from JournalDev!”, and we’re trying to tokenize it by spaces.

因此,我们有输入字符串“ Hello from JournalDev!”,我们正在尝试用空格标记它。

We get the first token using strtok(input, " "). Notice the double quotes, as the delimiter is a single character string!

我们使用strtok(input, " ")获得第一个令牌。 注意双引号,因为定界符是单个字符串!

Afterwards, we keep getting tokens using strtok(NULL, " ") and loop until we get NULL from strtok().

之后,我们继续使用strtok(NULL, " ")获取令牌并循环,直到从strtok()获取NULL 。

Let’s look at the output now.

现在让我们看一下输出。

Output

输出量

Token List:HellofromJournalDev!

Indeed, we seem to have got the correct tokens!

确实,我们似乎已经获得了正确的令牌!

Similarly, let’s now look at using strtok_r().

同样,让我们​​现在来看一下使用strtok_r() 。



使用strtok_r()函数 (Using the strtok_r() function)

This function is very similar to the strtok() function. The key difference is that the _r means that this is a re-entrant function.

此函数与strtok()函数非常相似。 关键区别在于_r表示这是可重入函数。

A reentrant function is a function that can be interrupted during its execution. This type of function can also be safely called again, to resume execution!

可重入函数是可以在其执行期间中断的函数。 也可以再次安全地调用此类函数,以恢复执行!

This is why it is a “re-entrant” function. Just because it can safely enter again!

这就是为什么它是“可重入”功能的原因。 只是因为它可以安全地再次进入!

Due to this fact, re-entrant functions are thread-safe, meaning that they can safely be interrupted by threads, just because they can resume again without any harm.

因此,重入函数是线程安全的,这意味着它们可以安全地被线程中断,因为它们可以再次恢复而不会造成任何伤害。

Now, similar to strtok(), the strtok_r() function is a thread-safe version of it.

现在,类似于strtok() , strtok_r()函数是该线程的线程安全版本。

However, this has an extra parameter to it, called the context. We need this, so that the function can resume from the right place.

但是,这有一个额外的参数,称为context 。 我们需要这样做,以便函数可以从正确的位置恢复。

NOTE: If you’re using Windows, the equivalent function is strtok_s(). strtok_r() is for Linux / Mac based systems!

注意 :如果使用的是Windows,则等效功能为strtok_s() 。 strtok_r()适用于基于Linux / Mac的系统!

#include <string.h>char *strtok_r(char *str, const char *delim, char **context);

The context parameter is a pointer to the character, which strtok_r uses internally to save its state.

context参数是指向字符的指针, strtok_r内部使用该指针保存其状态。

Usually, we can just pass it from a user-declared pointer.

通常,我们可以从用户声明的指针传递它。

Let’s look at the same example for strtok(), now using strtok_r() (or strtok_s() on Windows).

让我们看一下strtok()的相同示例,现在使用strtok_r() (或Windows上的strtok_s() )。

#include <stdio.h>#include <string.h>int main() { // Our input string char input_string[] = "Hello from JournalDev!"; // Our output token list char token_list[20][20]; // A pointer, which we will be used as the context variable // Initially, we will set it to NULL char* context = NULL; // To get the value of the context variable, we can pass it's address // strtok_r() to automatically populate this context variable, and refer // it's context in the future char* token = strtok_r(input_string, " ", &context); int num_tokens = 0; // Index to token list. We will append to the list while (token != NULL) { // Keep getting tokens until we receive NULL from strtok() strcpy(token_list[num_tokens], token); // Copy to token list num_tokens++; token = strtok_r(NULL, " ", &context); // We pass the context variable to strtok_r } // Print the list of tokens printf("Token List:n"); for (int i=0; i < num_tokens; i++) { printf("%sn", token_list[i]); } return 0;}

Output

输出量

Token List:HellofromJournalDev!

While we get the same output, this version is better, since it is thread safe!

虽然我们得到相同的输出,但是此版本更好,因为它是线程安全的!



结论 (Conclusion)

In this article, we learned about how we could use the strtok() and strtok_r() functions in C, to tokenize strings easily.

在本文中,我们学习了如何在C中使用strtok()和strtok_r()函数轻松地对字符串进行标记。

For similar content, do go through our tutorial section on C programming!

对于类似的内容,请阅读我们有关C编程的教程部分!

参考资料 (References) Linux manual page on strtok() and strtok_r() functions in C

有关C语言中strtok()和strtok_r()函数的Linux手册页



翻译自: https://www.journaldev.com/42293/strtok-strtok-r-functions-c

c语言中strtok函数

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。