//
// https://www.programiz.com/c-programming/online-compiler/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
char **split(char *string, char * splitter, int *count);
int main() {
char **ptr;
int nr_ptrs=0;
char a[]="To be, or (not) to be, that is the question.";
char separators[]=",(). ";
printf("Original string: %s\nSeparators: %s\n", a, separators);
ptr=split(a, separators, &nr_ptrs);
for (int i=0; i < nr_ptrs; i++) {
// printf("%c\n", *ptr[i]); // every character of a
printf("%s\n", ptr[i]); // pointer moving up a
}
free(ptr);
return 0;
}
char **split(char *string, char * splitter, int *count) {
char *ptr;
bool first=true;
int j=0;
int len = strlen(string);
// dynamically declared pointer array as return value
char **p=(char **)malloc((len) * sizeof(char *));
for (int i=0; i < len; i++) {
// search for char of string in splitter string
ptr=strchr(splitter,string[i]);
// char not in splitter, set pointer 1 time
if (ptr == NULL) {
if (first) {
p[*count] = &string[i];
(*count)++;
first=false;
}
} else { // char in splitter, marks end of substring with \0
string[i] = '\0';
first = true;
}
}
return p;
}