-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
87 lines (78 loc) · 1.18 KB
/
ft_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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "libft.h"
static int numstring(char const *s1, char c)
{
int comp;
int cles;
comp = 0;
cles = 0;
if (*s1 == '\0')
return (0);
while (*s1 != '\0')
{
if (*s1 == c)
cles = 0;
else if (cles == 0)
{
cles = 1;
comp++;
}
s1++;
}
return (comp);
}
static int numchar(char const *s2, char c, int i)
{
int lenght;
lenght = 0;
while (s2[i] != c && s2[i] != '\0')
{
lenght++;
i++;
}
return (lenght);
}
char **freee(char **dst, int j)
{
while (j > 0)
{
j--;
free((void *)dst[j]);
}
free(dst);
return (NULL);
}
static char **affect(char const *s, char **dst, char c, int l)
{
int i;
int j;
int k;
i = 0;
j = 0;
while (s[i] != '\0' && j < l)
{
k = 0;
while (s[i] == c)
i++;
dst[j] = (char *)malloc(sizeof(char) * numchar(s, c, i) + 1);
if (dst[j] == NULL)
return (freee(dst, j));
while (s[i] != '\0' && s[i] != c)
dst[j][k++] = s[i++];
dst[j][k] = '\0';
j++;
}
dst[j] = 0;
return (dst);
}
char **ft_split(char const *s, char c)
{
char **dst;
int l;
if (s == NULL)
return (NULL);
l = numstring(s, c);
dst = (char **)malloc(sizeof(char *) * (l + 1));
if (dst == NULL)
return (NULL);
return (affect(s, dst, c, l));
}