String Splitting Revisited Part I
Here in part one I will go through a way to split a string by a delimiter. This delimiter is a string with a single character.
Headers:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <xstdlib.h>
#include <string.h>
#include <stdlib.h>
#include <xstdlib.h>
Prototype:
char ** split(char *delimiter, char *str);
Function code:
char ** split(char *delimiter, char *str) {
char **elements = (char **) calloc(1, sizeof(char));
char **_elements = NULL;
/*
* x to track strpos offset in *str and e for element tick and current delimiter position, i for var
* start for the start of the substr and stop for the end
*/
int x = 0, e = 0, d = 0, next_d = -1, start, stop, str_size = (int) strlen(str);
while ((d = strpos(delimiter, str, x)) != -1) {
if ((next_d = strpos(delimiter, str, x + 1)) == -1) next_d = str_size;
if (x == 0 && str[0] != str[d] && e == 0) {
/* lets not skip the first group */
next_d = d, d = 0, start = d, stop = next_d;
} else if (x == 0 && str[0] == str[d] && e == 0) {
/* this runs if the delimiter is the first character in the search */
next_d = 1, start = 0, stop = 0;
} else {
/* increment d so that we can start from the proper pos in substr */
x++, start = d + 1, stop = (next_d - d) - 1;
}
elements[e] = (char *) calloc(next_d - d, sizeof(char));
strcpy(elements[e], substr(str, start, stop));
/* debugging information */
//printf("%s\n", elements[e]);
_elements = realloc(elements, (e + 2) * sizeof(char *));
elements = _elements;
e++;
}
return elements;
}
char **elements = (char **) calloc(1, sizeof(char));
char **_elements = NULL;
/*
* x to track strpos offset in *str and e for element tick and current delimiter position, i for var
* start for the start of the substr and stop for the end
*/
int x = 0, e = 0, d = 0, next_d = -1, start, stop, str_size = (int) strlen(str);
while ((d = strpos(delimiter, str, x)) != -1) {
if ((next_d = strpos(delimiter, str, x + 1)) == -1) next_d = str_size;
if (x == 0 && str[0] != str[d] && e == 0) {
/* lets not skip the first group */
next_d = d, d = 0, start = d, stop = next_d;
} else if (x == 0 && str[0] == str[d] && e == 0) {
/* this runs if the delimiter is the first character in the search */
next_d = 1, start = 0, stop = 0;
} else {
/* increment d so that we can start from the proper pos in substr */
x++, start = d + 1, stop = (next_d - d) - 1;
}
elements[e] = (char *) calloc(next_d - d, sizeof(char));
strcpy(elements[e], substr(str, start, stop));
/* debugging information */
//printf("%s\n", elements[e]);
_elements = realloc(elements, (e + 2) * sizeof(char *));
elements = _elements;
e++;
}
return elements;
}
Example use:
int main(void) {
system("clear");
char *text = "This,is,split,by,a,comma,delimiter!";
char **s = split(",", text);
return 0;
}
system("clear");
char *text = "This,is,split,by,a,comma,delimiter!";
char **s = split(",", text);
return 0;
}
From that example you would just have to loop through char **s and print your results. In part 2 I will show you how to split a string using a delimiter that has more than one character. The code is a bit different but it will make the function above more powerful.
Enjoy,
Rashaud
Categories: C/C++