【发布时间】:2021-11-17 10:23:48
【问题描述】:
我有这行文字:
32+-#3#2-#3#3
我需要将数字彼此分开。所以基本上结果是这样的:
3
2+-
3
2-
3
3
这是我的代码,但它不能正常工作,因为我有两位数字:
#include <stdio.h>
#include <string.h>
int main(void) {
char string[50] = "32-#3#2-#3#3";
// Extract the first token
char *token = strtok(string, "#");
// loop through the string to extract all other tokens
while (token != NULL) {
printf(" %s\n", token); //printing each token
token = strtok(NULL, "#");
}
return 0;
}
【问题讨论】:
-
那你为什么不一次读一个字符然后跳过分隔符呢?
-
所以“32”不被认为是“三十二”?然后我会用“数字”而不是“数字”来表达。
-
“这是我的代码,但它不能正常工作,因为我有两位数字” 我们无法猜测“不能正常工作”是什么意思。您能否至少指定一个示例输入和结果输出,并清楚地解释为什么输出与您想要的不同?另见:What do you mean "It doesn't work?"
标签: c algorithm split strsplit