首页 > 后端开发 > C++ > 在C++中实现strtok()函数

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

WBOY
发布: 2023-09-01 19:21:04
转载
1548 人浏览过

在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 (`'

  • 第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 <iostream>
#include <cstring> // 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 << ptr << endl;
      ptr = strtok(NULL, " ");
   }
   return 0;
}
登录后复制

Output

Hi, 
this  
topic 
tells  
about 
strtok()  
function 
登录后复制

方法二

下面是使用循环和strtok()函数以多个分隔符显示代码的程序。下面是相同的程序代码。

The '|' and'' (space) delimiters are used to tokenize the string "The boy|is|standing|with his pet|lazy dog." in this example. Once with the string as the first argument and the characters '|' and'' as the second argument, the strtok() method is called twice. Following calls return the remaining tokens, the initial call returns the first token, "The." Until all tokens have been extracted, the loop keeps running.

Example-2

#include <iostream> 
#include <cstring> 
 
int main() { 
   char str[] = "The boy|is|standing|with his pet|lazy dog."; 
   char* token = strtok(str, "| "); 
   while (token != NULL) { 
      std::cout << token << std::endl; 
      token = strtok(NULL, "| "); 
   } 
   return 0; 
} 
登录后复制

Output

The 
boy 
is 
standing 
with 
his 
pet 
lazy 
dog.
登录后复制

结论

总之,C++的strtok函数是一个有用且有效的用于操作字符串的工具。尽管它有一些重要的限制和潜在的缺点,但它仍然是解析和处理文本数据的常见选择,并且对于任何编码人员来说都是一个有用的工具。

以上是在C++中实现strtok()函数的详细内容。更多信息请关注PHP中文网其他相关文章!

相关标签:
来源:tutorialspoint.com
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板