现在的位置: 首页 > 综合 > 正文

strtok函数

2013年09月26日 ⁄ 综合 ⁄ 共 835字 ⁄ 字号 评论关闭

函数原型:extern char *strtok(char *string, char *seps)

参数说明:string为源字符串,seps为指定的分隔符,是一个分隔字符串的集合。
       
所在库名:#include <string.h>
 
函数功能:将字符串string中所有在seps中出现的分隔符替换掉。
 
返回说明:返回指向下一个标记串。当没有标记串时则返回空字符NULL。

其它说明:

 当第一次调用strtok函数的时候,strtok函数跳过seps中的第一个分隔符,同时返回在string中的出现的第一个分隔符的位置的指针,用一个空字符'/0'终止。

通过循环,经过多次调用strtok函数,seps中更多的分隔字符都会被'/0'替换掉,最终输出我们意图实现的字符串。

实例:

/* MSDN提供 */
#include <string.h>
#include 
<stdio.h>

char string[] = "A string/tof ,,tokens/nand some  more tokens";
char seps[]   = " , /t/n";
char *token;

void main( void )
{
   printf( 
"%s Tokens: "string );   
   token 
= strtok( string, seps );   /* Establish string and get the first token: */
   
while( token != NULL )
   
{
            printf( 
" %s ", token );   /* While there are tokens in "string" */
           token 
= strtok( NULL, seps );   /* Get next token: */
   }

}

在VC++ 6.0  编译运行:

抱歉!评论已关闭.