需要在每次调用strtok_s函数时去做判断,有没有更优的解决方法啊?

直接用代码描述
string str = "abc|def|gh||mno";
对以上字符串使用strtok_s函数,结果为
abc,def,gh,mno
但自己想得到的是
abc,def,gh,,mno //注意,gh后面是两个逗号
拜托了,谢谢
现在是有办法去实现,就是在调用该函数的同时去判断剩余的字符串内容的首字符是否为标识符,但这样就需要在每次调用strtok_s函数时去做判断,有没有更优的解决方案呢?求教

慕虎7371278
浏览 114回答 2
2回答

婷婷同学_

char sentence[]="This|is|a|sentence|with|7|||tokens";cout<<"The string to be tokenized is:\n"<<sentence<<"\n\nThe tokens are:\n\n";char *tokenPtr=strtok(sentence,"|");while( tokenPtr ){cout<<tokenPtr<<" ";tokenPtr=strtok(NULL,"|");}cout<<endl;cout<<"After strtok, sentence="<<sentence<<endl;用strtok 就可以了

江户川乱折腾

自己写程序实现,不要总是依赖于现成的库函数,他们的效率与安全性都不能得到保障的。
打开App,查看更多内容
随时随地看视频慕课网APP