-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
63 lines (58 loc) · 1.56 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: lkuenane <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/06/06 04:39:28 by lkuenane #+# #+# */
/* Updated: 2017/06/07 17:07:47 by lkuenane ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_words(char const *s, char c)
{
int i;
size_t size;
i = 0;
size = 0;
while (s[i] && s[i] == c)
i++;
while (s[i])
{
while (s[i] && s[i] == c)
i++;
if (s[i])
{
while (s[i] && s[i] != c)
i++;
size++;
}
}
return (size);
}
char **ft_strsplit(char const *s, char c)
{
char **tab;
size_t size;
int i;
int start;
if (!s || !(tab = (char **)malloc(sizeof(char *) * (ft_words(s, c) + 1))))
return (NULL);
i = 0;
size = 0;
while (s[i])
{
if (s[i] == c)
i++;
else
{
start = i;
while (s[i] && s[i] != c)
i++;
tab[size++] = ft_strsub(s, start, i - start);
}
}
tab[size] = NULL;
return (tab);
}