strtok() 函数

本文介绍了如何使用C语言中的strtok函数来解析字符串。通过示例代码详细展示了如何设置分隔符并逐个获取字符串中的标记。此函数对于文本处理和数据解析非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 

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"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值