C语言字符串拆分函数strtok

简介:

char *  strtok ( char * string, const char * delimiters );

Sequentially truncate string if delimiter is found.
  If string is not NULL, the function scans string for the first occurrence of any character included in delimiters. If it is found, the function overwrites the delimiter in string by a null-character and returns a pointer to the token, i.e. the part of the scanned string previous to the delimiter.
  After a first call to strtok, the function may be called with NULL as string parameter, and it will follow by where the last call to strtok found a delimiter.
  delimiters may vary from a call to another.

Parameters.

string
Null-terminated string to scan.
separator
Null-terminated string containing the separators.

Return Value.
  A pointer to the last token found in string.   NULL is returned when there are no more tokens to be found.

Portability.
  Defined in ANSI-C.

Example.

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="This is a sample string,just testing.";
  char * pch;
  printf ("Splitting string \"%s\" in tokens:\n",str);
  pch = strtok (str," ");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.");
  }
  return 0;
}

Output:
Splitting string "This is a sample string,just testing." in tokens:
This
is
a
sample
string
just
testing 

本文转自feisky博客园博客,原文链接:http://www.cnblogs.com/feisky/archive/2010/05/25/1743274.html,如需转载请自行联系原作者



相关文章
|
5天前
|
存储 C语言
【C语言函数】static和extern关键字修饰
【C语言函数】static和extern关键字修饰
|
6天前
|
C语言 C++
|
3天前
|
测试技术 C语言
C语言中的void函数
C语言中的void函数
|
3天前
|
存储 安全 编译器
C语言中的scanf函数
C语言中的scanf函数
|
3天前
|
存储 搜索推荐 C语言
C语言中的指针函数:深入探索与应用
C语言中的指针函数:深入探索与应用
|
3天前
|
C语言
C语言中的无参函数
C语言中的无参函数
|
5天前
|
C语言
C语言------函数
这篇文章是C语言中函数的实训,涵盖了函数的定义、调用、自定义函数编写以及递归调用方法,并通过多个示例代码演示了如何实现累加、阶乘、斐波那契数列、特殊数列求和等函数功能。
C语言------函数
|
6天前
|
编译器 C语言
【C语言小知识】ctype.h系列的字符函数
【C语言小知识】ctype.h系列的字符函数
|
5天前
|
存储 程序员 编译器
[C语言]函数
[C语言]函数
|
10天前
|
编译器 C语言
C语言函数的学习
掌握函数的使用是学习C语言的关键一环,理解和应用这些基本的函数概念将使你能够更有效地利用C语言的强大功能。
7 0