strtok 使用了错误的分隔符
当我将分隔符指定为“,”时,为什么我的 strtok
会在空格后分解字符串?
Why is my strtok
breaking up my strings after space when I specified my delimiter as ","?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我只能建议你做错了什么,尽管很难确切地说出什么(你通常应该在询问细节时发布你的代码)。示例程序(如下所示)似乎工作正常:
它输出:
立即想到的唯一可能性是,如果您使用
" ,"
而不是","
。在这种情况下,您将得到:I can only suggest that you're doing something wrong though it's a little hard to tell exactly what (you should generally post your code when asking about specifics). Sample programs, like the following, seem to work fine:
It outputs:
The only possibility that springs to mind immediately is if you're using
" ,"
instead of","
. In that case, you would get:谢谢!我环顾四周,发现问题出在我的 scanf 上,它没有读取用户输入的整行。看来我的 strtok 工作正常,但我用来匹配 strtok 返回值的值是错误的。
例如,我的 strtok 函数采用“Jeremy Whitfield,Ronny Whitfield”并给出“Jeremy Whitfield”和“Ronny Whitfield”。在我的程序中,我使用 scanf 来接收用户输入 > “Ronny Whitfield”实际上只是读“Ronny”。所以这是我的 scanf 而不是 strtok 的问题。
我的虚拟机每次打开时都会卡住,所以我现在无法访问我的代码。
Thanks! I looked around and figured out that the problem was with my scanf which doesn't read the whole line the user inputs. It seems that my strtok was working fine but the value i am using to match the return value of strtok is wrong.
For example, my strtok function takes "Jeremy whitfield,Ronny Whifield" and gives me "Jeremy Whitfield" and "Ronny Whitfield". In my program, i am using scanf to take in user input > "Ronny Whitfield" which is actually only reading "Ronny". So its a problem with my scanf not strtok.
My virtual machine is getting stuck everytime i open it so i am unable to access my code for now.