C 语言中的字符串标记函数
在本节中,我们将了解如何在 C 中标记字符串。C 有此功能库。C 库函数 char *strtok(char *str, const char *delim) 将字符串 str 使用分隔符 delim 分解成一系列标记。
以下是 strtok() 函数的声明。
char *strtok(char *str, const char *delim)
它接受两个参数:str - 该字符串的内容经过修改并分解为更小的字符串(标记),以及 delim - 这是包含分隔符的 C 字符串。在一次调用到另一次调用之间,它们可能不同。此函数返回第一个在字符串中找到的标记的指针。如果没有可检索标记,则返回空指针。
示例代码
#include <string.h>
#include <stdio.h>
int main () {
char str[80] = "This is - www.tutorialspoint.com - website";
const char s[2] = "-";
char *token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL ) {
printf( " %s
", token );
token = strtok(NULL, s);
}
return(0);
}输出
This is www.tutorialspoint.com website
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP