strtok的行为 [英] strtok behavior

查看:144
本文介绍了strtok的行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 int main ()
 {

    char str[] ="kk,12,,23,4,,,3434,3,33,,,";
    char * valarr;
    int count=0;
    valarr = strtok(str,","); 
    while(valarr != '\0')
    {
               valarr = strtok(NULL,","); 
    count++;
    }
    printf("%d\n",count);
   return 0;
  }

在以上程序的输出为7。

In above program the output is 7.

这似乎是strtok函数在一次连续标记化的逗号。

It seems that the strtok is tokenizing consecutive commas at once.

而不是连续的逗号的,我可以在两者之间引入一个空白,但
有没有办法来克服这种让我有空也计数?

Instead of consecutive commas I can introduce a blank in between but Is there a way to overcome this so that I have empty space also in the count ?

推荐答案

正确的。 文档状态这pretty清楚:

Correct. The documentation states this pretty clearly:

的两个或更多个连续分隔符分析的字符串中的序列被认为是一个单一的分隔符。

A sequence of two or more contiguous delimiter characters in the parsed string is considered to be a single delimiter.

这是多么的strtok()应该工作。你可能会滚动自己的好,这也将释放您从的strtok()的污秽。

That's just how strtok() is supposed to work. You might be better of rolling your own, which will also free you from strtok()'s nastiness.

这篇关于strtok的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆