我有这个函数:int split(char* str, char s)
,那么如何在不使用 strtok()
的情况下拆分 str
> 还是其他功能?
例如:str = "1,2,3,4,5", s = ','
split(str, s)
后,输出为:
1
2
3
4
5
抱歉,如果 str == NULL 则 int 返回 -1,如果 str != NULL 则返回 1。
最佳答案
这个怎么样?我不确定 int 返回类型在函数中意味着什么,所以我将其设为拆分计数。
#include <stdio.h>
int split(char* str, char s) {
int count = 0;
while (*str) {
if (s == *str) {
putchar('\n');
count++;
} else {
putchar(*str);
}
str++;
}
return count;
}
关于c++ - 如何在不使用任何基本函数的情况下拆分字符数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34408056/