×
新网 > 建站推广 > 正文

strtok函数的用法详解

  • 作者:zccc
  • 来源:网络
  • 2020-07-09 09:03:17

strtok函数用于分解字符串为一组字符串,strtok函数的声明是“char *strtok(char *str, const char *delim)”,其中参数str表示要被分解成一组小字符串的字符串。C 库函数 - st

strtok函数用于分解字符串为一组字符串,strtok函数的声明是“char *strtok(char *str, const char *delim)”,其中参数str表示要被分解成一组小字符串的字符串。

C 库函数 - strtok()

描述

C 库函数 char *strtok(char *str, const char *delim) 分解字符串 str 为一组字符串,delim 为分隔符。

声明

下面是 strtok() 函数的声明。

char *strtok(char *str, const char *delim)

参数

str -- 要被分解成一组小字符串的字符串。

delim -- 包含分隔符的 C 字符串。

返回值

该函数返回被分解的第一个子字符串,如果没有可检索的字符串,则返回一个空指针。

实例

下面的实例演示了 strtok() 函数的用法。

实例

#include <string.h>
#include <stdio.h>
 
int main () {
   char str[80] = "This is - www.runoob.com - website";
   const char s[2] = "-";
   char *token;
   
   /* 获取第一个子字符串 */
   token = strtok(str, s);
   
   /* 继续获取其他的子字符串 */
   while( token != NULL ) {
      printf( "%s\\n", token );
    
      token = strtok(NULL, s);
   }
   
   return(0);
}

让我们编译并运行上面的程序,这将产生以下结果:

This is 
 www.runoob.com 
 website

推荐:《C语言教程》

  • 相关专题

免责声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,也不承认相关法律责任。如果您发现本社区中有涉嫌抄袭的内容,请发送邮件至:operations@xinnet.com进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。

免费咨询获取折扣

Loading