-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_split.c
61 lines (56 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/07 17:02:38 by hbaddrul #+# #+# */
/* Updated: 2021/11/21 20:30:08 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static size_t ft_toklen(const char *s, char c)
{
size_t ret;
ret = 0;
while (*s)
{
if (*s != c)
{
++ret;
while (*s && *s != c)
++s;
}
else
++s;
}
return (ret);
}
char **ft_split(const char *s, char c)
{
char **ret;
size_t i;
size_t len;
if (!s)
return (0);
i = 0;
ret = malloc(sizeof(char *) * (ft_toklen(s, c) + 1));
if (!ret)
return (0);
while (*s)
{
if (*s != c)
{
len = 0;
while (*s && *s != c && ++len)
++s;
ret[i++] = ft_substr(s - len, 0, len);
}
else
++s;
}
ret[i] = 0;
return (ret);
}