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

strtok() 函数

2013年09月07日 ⁄ 综合 ⁄ 共 550字 ⁄ 字号 评论关闭
 

strtok

语法:

 

  #include <string.h>
  char *strtok( char *str1, const char *str2 );

功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。

例如:

    char str[] = "now # is the time for all # good men to come to the # aid of their country";
    char delims[] = "#";
    char *result = NULL;
 

    result = strtok( str, delims );
 

    while( result != NULL ) {
        printf( "result is /"%s/"/n", result );
         result = strtok( NULL, delims );
    }

以上代码的运行结果是:

    result is "now "
    result is " is the time for all "
    result is " good men to come to the "
    result is " aid of their country"

抱歉!评论已关闭.