-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.c
42 lines (35 loc) · 842 Bytes
/
split.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
// By: Lisheng Zhou
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "split.h"
// This function splits a string (s1) by a delimiter (s2)
char **split ( const char *s1, const char *s2) {
char **lista;
char *aux = (char*)malloc(strlen(s1) + 1);
strcpy(aux, s1);
char *token_Ptr;
int i = 0;
lista = (char **) malloc (sizeof (char *));
token_Ptr = strtok(aux, s2);
lista[i] = token_Ptr;
i++;
while(token_Ptr != NULL)
{
lista = (char **)realloc(lista, sizeof(char*) * (i + 1));
token_Ptr = strtok(NULL, s2);
lista[i] = token_Ptr;
i++;
}
return lista;
}
/* Example
int main(){
char **list;
list = split("1,2,3,4,5", ",");
printf ("%s\n", list[4]);
free(list[0]);
free(list);
return 0;
}
*/