-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
63 lines (58 loc) · 1.64 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: igarbuz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/09 14:28:24 by igarbuz #+# #+# */
/* Updated: 2018/11/16 14:14:32 by igarbuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_strdp(char const *mys, char c)
{
int i;
char *mystr;
i = 0;
while (mys[i] != c && mys[i])
i++;
if (!(mystr = (char *)malloc(sizeof(char) * (i + 1))))
return (NULL);
i = 0;
while (*mys != c && *mys)
{
mystr[i] = *mys;
i++;
mys++;
}
mystr[i] = '\0';
return (mystr);
}
char **ft_strsplit(char const *s, char c)
{
char **mytbl;
int i;
char const *tmp;
if (!s || !c)
return (NULL);
i = 0;
tmp = s - 1;
while (*++tmp)
if (*tmp != c && (*(tmp + 1) == c || *(tmp + 1) == '\0'))
i++;
if (!(mytbl = (char **)malloc(sizeof(char *) * (i + 1))))
return (NULL);
i = 0;
while (*s)
{
while (*s == c)
s++;
if (*s)
mytbl[i++] = ft_strdp(s, c);
while (*s != c && *s)
s++;
}
mytbl[i] = 0;
return (mytbl);
}