-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
70 lines (63 loc) · 1.63 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
62
63
64
65
66
67
68
69
70
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wtaylor <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/09 13:56:13 by wtaylor #+# #+# */
/* Updated: 2020/11/09 20:59:19 by wtaylor ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_howmany(char const *s, char c)
{
int i;
i = 0;
while (*s)
{
if (*s != c && (*(s + 1) == c || *(s + 1) == '\0'))
i++;
s++;
}
return (i);
}
static char **ft_free(char **arr)
{
int i;
i = 0;
while (arr[i])
{
free(arr[i]);
i++;
}
free(arr);
return (NULL);
}
char **ft_split(char const *s, char c)
{
char **arr;
int wrdlen;
int newwrd;
newwrd = 0;
if (!s)
return (NULL);
if (!(arr = (char **)ft_calloc((ft_howmany(s, c) + 1), sizeof(char *))))
return (NULL);
while (*s)
{
wrdlen = 0;
while (*s == c && *(s))
s++;
while (*s != c && *s)
{
wrdlen++;
s++;
}
if (wrdlen)
if (!(arr[newwrd] = ft_substr(s - wrdlen, 0, wrdlen)))
return (ft_free(arr));
newwrd++;
}
return (arr);
}