-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
59 lines (53 loc) · 820 Bytes
/
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
#include "libft.h"
static int count_word(char const *s, char c)
{
int count;
count = 0;
while (*s)
{
while (*s && *s == c)
++s;
if (*s && *s != c)
{
++count;
++s;
}
while (*s && *s != c)
++s;
}
return (count);
}
static char *a_string(const char *s, char c)
{
int i;
char *str;
i = 0;
while (s[i] && s[i] != c)
++i;
str = ft_calloc(i + 1, sizeof(char));
if (str == NULL)
return (NULL);
ft_memcpy(str, s, i);
return (str);
}
char **ft_split(char const *s, char c)
{
char **ptr;
int count;
int i;
count = count_word(s, c);
ptr = (char **)ft_calloc(count + 1, sizeof(char *));
if (ptr == NULL)
return (NULL);
i = 0;
while (*s)
{
while (*s && *s == c)
++s;
if (*s && *s != c)
ptr[i++] = a_string(s, c);
while (*s && *s != c)
++s;
}
return (ptr);
}