- int count_delimiters(char str[], const char* delimiters) {
- int i, j, result = 0;
- for (i = 0; i < strlen(str); ++i) {
- for (j = 0; j < strlen(delimiters); ++j) {
- if (str[i] == delimiters[j]) {
- ++result;
- }
- }
- }
- return (result + 1);
- }
- char** split(char str[], const char* delimiters) {
- int result_size = count_delimiters(str, delimiters);
- int i = 0;
- char* result[result_size];
- char* pch = strtok(str, ",");
- while (pch != NULL) {
- result[i] = pch;
- pch = strtok(NULL, ",");
- ++i;
- }
- return result;
- }
複製代碼 |