-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
73 lines (66 loc) · 1.9 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
71
72
73
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nkawaguc <nkawaguc@student.42tokyo.jp> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/22 13:04:36 by nkawaguc #+# #+# */
/* Updated: 2024/05/03 17:52:02 by nkawaguc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void free_rec(char **to_free, int num)
{
int i;
i = -1;
while (++i < num)
free(to_free[i]);
free(to_free);
}
static int split_fill(char **ret, int cnt, const char *s, char c)
{
int block;
int pos;
block = -1;
pos = 0;
while (++block < cnt)
{
while (s[pos] == c && s[pos] != '\0')
pos++;
s = s + pos;
pos = 0;
while (s[pos] != '\0' && s[pos] != c)
pos++;
ret[block] = (char *)malloc((pos + 1) * sizeof(char));
if (!ret[block])
return (free_rec(ret, block), 0);
pos = 0;
while (s[pos] != '\0' && s[pos] != c)
{
ret[block][pos] = s[pos];
pos++;
}
ret[block][pos] = '\0';
}
return (1);
}
char **ft_split(const char *s, char c)
{
int cnt;
int i;
char **ret;
if (!s)
return (NULL);
i = 0;
cnt = (s[0] != c && s[0] != '\0');
while (s[0] != '\0' && s[++i] != '\0')
cnt += (s[i - 1] == c && s[i] != c);
ret = (char **)malloc((cnt + 1) * sizeof(char *));
if (!ret)
return (NULL);
ret[cnt] = NULL;
if (!split_fill(ret, cnt, s, c))
return (NULL);
return (ret);
}