-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
75 lines (69 loc) · 1.6 KB
/
ft_strsplit.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alaulom <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/15 18:03:01 by alaulom #+# #+# */
/* Updated: 2014/11/15 19:09:44 by alaulom ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int wc(const char *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[i])
{
if (s[i] != c)
{
++j;
while (s[i] != c)
{
if (!s[i])
return (j);
++i;
}
}
++i;
}
return (j);
}
static int wl(const char *s, int i, int j, char c)
{
while (s[i] != c && s[i])
{
++i;
++j;
}
return (j);
}
char **ft_strsplit(const char *s, char c)
{
char **ret;
int i;
int k;
int w;
if (!s)
return (0);
w = wc(s, c);
ret = (char **)malloc(sizeof(char *) * w + 1);
if (!ret)
return (NULL);
i = 0;
k = 0;
ret[w] = 0;
while (k < w)
{
while (s[i] == c)
++i;
ret[k] = ft_strsub(s, i, wl(s, i, 0, c));
while (s[i] && s[i] != c)
i++;
++k;
}
return (ret);
}