在C++中实现strtok()函数

2023年 9月 1日 30.0k 0

在C++中实现strtok()函数

strtok()函数是C++中最常用的函数之一。使用分隔符作为指导,该函数可以将文本分割成较小的块或标记。由于strtok()函数的存在,使用字符串在C++中变得简单。本文将对strtok()函数进行详细的讲解,包括其定义、语法、算法和各种实现策略。需要记住的是,strtok函数有一些限制和潜在的缺点。例如,它不能用于const或只读字符串,因为它会直接改变原始字符串。边缘情况和意外输入,如空字符串或在序列中重复出现的分隔符,也可能难以处理。

尽管存在这些缺点,但这个函数仍然是许多程序员的有用工具,并且经常被广泛应用于各种应用程序中,包括文本处理、数据解析和网络协议。它是一个灵活而强大的函数,可以显著简化许多常规编程任务。

Characteristics of strtok() function

To split a string into smaller chunks or tokens based on a delimiter, use the strtok() function in C++. A pointer to the string that has to be tokenized and a string with the delimiter characters are the two inputs for the function. A pointer to the string's very first token is returned by the function. As long as there are tokens left in the string, successive calls to the function with the same string parameter will return additional tokens and this happens with the help of NULL pointer in while loop. When calling the'strtok()' function again, supplying NULL as the first argument instructs the function to pick up where it left off with the previous tokenization of the same string.

When the function'strtok()' is first called with the input string as the first argument, it starts looking for the first token in the string at the beginning. When it locates the initial token, it terminates it by replacing the delimiter that comes after it with the null character (''0''). When'strtok()' is used again with a NULL pointer as the first argument, the function picks up where it left off with the previous tokenization of the string, that is, directly after the previous token. It keeps looking for the delimiter's next appearance.

语法

Char* strtok(char* str, const char* delimiters);

登录后复制

Where

- `str` is a pointer to the string to be tokenized.

- `delimiters` 是一个包含分隔符字符的字符串。

算法

  • Step 1 − The `strtok()` function takes two arguments - the first argument is the input string to be tokenized and the second argument is a string containing all the delimiters that should be used to split the input string into tokens.

  • Step 2 − When the function is called for the first time, the input string is passed as the first argument, and the delimiter string is passed as the second argument.

  • 步骤 3 − 该函数在输入字符串中搜索任何一个分隔符字符的第一个出现

  • Step 4 − When it finds a delimiter character, it replaces it with a null terminator (`''`) and returns a pointer to the beginning of the first token.

  • 第5步 - 下次调用该函数时,第一个参数应设置为`NULL`,而不是原始输入字符串。这告诉函数继续从上次停下的地方开始,在字符串中搜索下一个标记。

  • Step 6 − The function continues to search for delimiter characters and returns pointers to the beginning of subsequent tokens until it reaches the end of the string, at which point it returns `NULL`.

Approaches to follow

方法1 − 使用循环和strtok()函数以及单个分隔符来展示代码的程序。

方法2 - 使用循环和strtok()函数以及多个分隔符来展示代码的程序

方法一

Below is the program to show code using a loop with strtok() function and single delimeter

这个示例演示了如何使用strtok按空格对字符串进行分词。输入字符串str首先与分隔符" "一起传递给strtok。第一次调用strtok返回指向第一个标记("Hi,")的指针,将其打印到控制台。strtok继续对字符串进行分词,直到没有更多的标记,并且每个标记都打印到控制台。请注意,对于后续对strtok的调用,我们将一个空指针作为第一个参数传递,表示我们要继续对原始字符串进行分词

示例1

#include
#include // Include the header for strtok()
using namespace std;

int main() {
char str[] = "Hi, this topic tells about strtok() function";
char* ptr;
ptr = strtok(str, " ");
while (ptr != NULL) {
cout

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论