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

在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.

立即学习“C++免费学习笔记(深入)”;

语法

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 (`’

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.

‘`) 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 << 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  #include   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()函数的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444244.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 21:15:20
下一篇 2025年12月17日 13:22:21

相关推荐

  • 使用C++找到图中的汇节点的数量

    在本文中,我们将描述解决图中汇节点数量的重要信息。在这个问题中,我们有一个有 N 个节点(1 到 N)和 M 个边的有向无环图。目标是找出给定图中有多少个汇节点。汇聚节点是不产生任何传出边的节点。这是一个简单的例子 – Input : n = 4, m = 2Edges[] = {{2,…

    2025年12月17日
    000
  • 使用C++移除给定数字中的重复数字

    在本文中,我们给出了一个数字 n,我们需要删除给定数字中的重复数字。 Input: x = 12224Output: 124Input: x = 124422Output: 1242Input: x = 11332Output: 132 在给定的问题中,我们将遍历所有数字并删除重复的数字。 寻找解决…

    2025年12月17日
    000
  • 如何在C语言中将数组中的单个元素作为参数传递给函数?

    如果要将单个元素作为参数传递,则在函数调用中必须给出数组元素及其下标。 为了接收这些元素,在函数定义中使用简单变量。 示例1 #includemain (){ void display (int, int); int a[5], i; clrscr(); printf (“enter 5…

    2025年12月17日
    000
  • 检查输入字符是字母、数字还是特殊字符在C中

    在本节中,我们将了解如何检查给定字符是数字、字母还是 C 中的某些特殊字符。 字母表是从 A – Z 和 a – z,然后数字为 0 – 9。所有其他字符均为特殊字符。因此,如果我们使用这些条件检查条件,我们可以轻松找到它们。 示例 #include #include main() { char c…

    2025年12月17日
    000
  • 当在C/C++中的数字常量前加上0时,这意味着它是一个八进制数

    有时我们可能会看到一些数字文字,其前缀为0。这表明该数字是八进制数。所以八进制文字在开头包含 0。例如,如果八进制数是 25,那么我们必须写 025。 示例 #include int main() { int a = 025; int b = 063; printf(“Decimal of 25(O…

    2025年12月17日
    000
  • 在C程序中,将等边三角形内切圆的面积计算出来

    在这里我们将看到等边三角形内切圆的面积。三角形的边是“a”。 等边三角形的面积 – 三角形的半周长是 – 所以圆的半径是 – 示例 #include #include using namespace std;float area(float a) { if (a …

    2025年12月17日 好文分享
    000
  • C/C++程序中的数组

    数组是一组固定数量的相同数据类型的项目。这些元素存储在内存中的连续内存位置中。 可以使用方括号“[]”和数组名称像a[4]、a[3]等从其索引值访问值的每个单个元素。 声明数组 在c/c++编程语言中,通过定义数组的类型和长度(元素数量)来声明数组。下面的语法显示了在c/c++中声明数组的方法− d…

    2025年12月17日
    000
  • 解释C语言中选择排序的过程

    选择排序是一种攻击性算法,用于从数组中找到最小的数字,然后将其放置到第一个位置。下一个要遍历的数组将从索引开始,靠近放置最小数字的位置。 选择排序的过程 选择元素列表中第一个最小的元素并将其放置在第一个位置。 对列表中的其余元素重复相同的操作,直到所有元素都获得已排序。 考虑以下列表 –…

    2025年12月17日 好文分享
    000
  • 使用C++编写,找到子数组中的质数数量

    在本文中,我们将描述查找子数组中素数数量的方法。我们有一个正数数组 arr[] 和 q 个查询,其中有两个整数表示我们的范围 {l, R},我们需要找到给定范围内的素数数量。下面是给定问题的示例 – Input : arr[] = {1, 2, 3, 4, 5, 6}, q = 1, L…

    2025年12月17日
    000
  • 在C语言中编写一个打印反向Floyd三角形的程序

    程序描述 弗洛伊德三角形是自然数的直角三角形数组,用于计算机科学教育。它以罗伯特·弗洛伊德的名字命名。它是通过用连续的数字填充三角形的行来定义的,从左上角的 1 开始 1 15 14 13 12 112 3 10 9 8 74 5 6 6 5 47 8 9 10 3 211 12 13 14 15 …

    2025年12月17日
    000
  • 在C++中的可重构数

    给定一个整数类型的值,假设为number。任务是检查给定的数字是否可重构。如果是,打印该数字是可重构数字,否则打印不可能。 什么是可重构数字? 当一个数字可以被其可用因子的总数整除时,它就是可重构的。例如,数字9是可重构的,因为它有3个因子(1、3、9),而9可以被3整除,因此它是一个可重构数字。 …

    2025年12月17日
    000
  • 计算菱形的面积和周长的程序,已知对角线是什么?在C++中,什么是菱形?

    什么是菱形? 在几何学中,菱形是四个边长相同的四边形。菱形与形状菱形相似。如果菱形的对角线成直角,那么它就变成正方形。 菱形的性质是 – 边相等对边平行,对角相等,是平行四边形对角线平分直角 下图是菱形 立即学习“C++免费学习笔记(深入)”; 问题 给定对角线,假设 d1 和 d2 的…

    2025年12月17日
    000
  • 使用C++反转一个双向链表

    在本文中,我们有一个双向链表,我们将解释在 C++ 中反转双向链表的不同方法。例如 – Input : {1, 2, 3, 4}Output : {4, 3, 2, 1} 通常会想到一种方法,但我们将使用两种方法 – 正常方法和非正统方法。 正常方法 在这种方法中,我们将经历…

    2025年12月17日
    000
  • C令牌是什么?

    这个C程序是一系列指令,每个指令都是一系列个体单元的集合。 C程序中的每个小个体单元通常被称为令牌,C程序中的每个指令都是令牌的集合。 令牌用于构建C程序,它们也被称为C程序的基本构建块。 在C程序中,令牌包含以下内容: 关键字标识符运算符特殊符号常量字符串数据值 在C程序中,所有这些关键字、标识符…

    2025年12月17日
    000
  • 在C语言中,while(1)和while(0)之间的区别是什么?

    我们知道在C语言中,’while’关键字用于定义一个循环,该循环根据传递给循环的条件来工作。现在,由于条件可以有两个值,即真或假,所以如果条件为真,则while块内的代码将被重复执行,如果条件为假,则代码将不会被执行。 现在,通过将参数传递给while循环,我们可以区分whi…

    2025年12月17日
    000
  • 在C语言中,是否可以在main()函数中传递参数?

    是的,我们可以在 main() 函数中给出参数。 C 中的命令行参数在系统命令行中的程序名称之后指定,这些参数值将传递给程序执行期间的程序。 argc 和 argv 是可以传递给 main 函数的两个参数。 但是当您从终端运行程序时,main() 函数实际上由操作系统(或 shell 程序)调用。 …

    2025年12月17日
    000
  • 如何在C中修改一个const变量?

    在C或C++中,我们可以使用常量变量。常量变量的值在初始化后就不能更改。在本节中,我们将了解如何更改某些常量变量的值。 如果我们想要更改常量变量的值,则会产生编译时错误。请检查以下代码以获得更好的想法。 示例 #include main() { const int x = 10; //define …

    2025年12月17日
    000
  • 使用C++找到Pell数

    在给定的问题中,我们得到一个整数 n,我们需要找到 Pn,即该位置的咒语编号。现在,正如我们所知,拼写数是由以下公式给出的序列的一部分 -Pn = 2*Pn-1 + Pn-2 前两个起始数字 – P0 = 0 和 P1 = 1 查找方法解决方案 现在我们将通过两种方法来解决这个问题:递归…

    2025年12月17日
    000
  • 在C语言中,预定义标识符__func__

    标识符是在编程中给实体赋予的名称,以在程序中进行标识。 通常,标识符是由程序员创建的,以实现高效工作,但也有一些预定义的标识符内置在编程中。例如,cout、cin等。 在这里,我们将看到C编程语言中的一个预定义标识符__func__。 __func__的正式定义为 − 立即学习“C语言免费学习笔记(…

    2025年12月17日
    000
  • c语言如何输出double类型

    c语言输出double类型的方法:1、使用printf函数输出,可以用于输出不同类型的值,包括double类型;2、使用fprintf函数输出到文件,使用fprintf函数可以将double类型的值输出到指定的文件中;3、使用sprintf函数输出到字符串,有时候,需要将double类型的值输出到一…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信