-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
70 lines (62 loc) · 1.84 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: aahlyel <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/13 22:57:13 by aahlyel #+# #+# */
/* Updated: 2022/10/27 00:47:31 by aahlyel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int word_count(char *s, char c);
static int alloc_fill_strs(char **splited, char *s, char c, int wc);
char **ft_split(char const *s, char c)
{
char **splited;
int wc;
if (!s)
return (NULL);
wc = word_count((char *)s, c);
splited = malloc((wc + 1) * sizeof(char *));
if (!splited)
return (NULL);
if (alloc_fill_strs(splited, (char *)s, c, wc))
{
while (*splited)
free(*splited++);
free(splited);
}
return (splited);
}
static int alloc_fill_strs(char **splited, char *s, char c, int wc)
{
int tmp_count;
while (wc--)
{
tmp_count = 0;
while (*s == c)
s++;
while (*(s + tmp_count) != c && *(s + tmp_count))
tmp_count++;
if (!tmp_count)
break ;
*splited = ft_substr(s, 0, tmp_count);
if (!splited)
return (1);
s += tmp_count;
splited++;
}
*splited = NULL;
return (0);
}
static int word_count(char *s, char c)
{
int i;
i = 0;
while (*(s++))
if (*(s - 1) != c && (*s == c || !*s))
i++;
return (i);
}