-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
86 lines (77 loc) · 1.76 KB
/
Copy pathft_split.c
File metadata and controls
86 lines (77 loc) · 1.76 KB
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
74
75
76
77
78
79
80
81
82
83
84
85
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rahidalg <rahidalg@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/05 12:01:03 by rahidalg #+# #+# */
/* Updated: 2024/04/18 11:54:13 by rahidalg ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **free_array(char **ptr, int i)
{
while (i > 0)
{
i--;
free(ptr[i]);
}
free(ptr);
return (0);
}
static size_t ft_arrlen(const char *s, char c)
{
size_t out;
out = 0;
while (*s)
{
if (*s != c)
{
out++;
while (*s && *s != c)
s++;
}
else
s++;
}
return (out);
}
static char **ft_checkstr(char **out, const char *s, char c)
{
size_t i;
size_t len;
i = 0;
while (*s)
{
if (*s != c)
{
len = 0;
while (*s && *s != c && s++)
len++;
out[i] = ft_substr(s - len, 0, len);
if (!out[i])
{
free_array(out, i);
return ((void *)0);
}
i++;
}
else
s++;
}
out[i] = NULL;
return (out);
}
char **ft_split(const char *s, char c)
{
char **out;
if (!s)
return ((void *)0);
out = malloc((ft_arrlen(s, c) + 1) * sizeof(char *));
if (!out)
{
return ((void *)0);
}
return (ft_checkstr(out, s, c));
}