-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
executable file
·94 lines (85 loc) · 1.86 KB
/
ft_split.c
File metadata and controls
executable file
·94 lines (85 loc) · 1.86 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
86
87
88
89
90
91
92
93
94
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kalshaer <kalshaer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/24 08:52:50 by kalshaer #+# #+# */
/* Updated: 2022/08/23 08:02:55 by kalshaer ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **hmalloc(char **tab)
{
int i;
i = 0;
while (tab[i])
{
free(tab[i]);
i++;
}
free(tab);
return (NULL);
}
static size_t countw(char const *s, char c)
{
int i;
int w;
i = 0;
w = 0;
if (!s[0])
return (0);
while (s[i] && s[i] == c)
i++;
while (s[i])
{
if (s[i] == c)
{
w++;
while (s[i] && s[i] == c)
i++;
continue ;
}
i++;
}
if (s[i - 1] != c)
w++;
return (w);
}
static char **min(char const *s, char c, char **r, int i)
{
size_t len;
while (*s)
{
while (*s == c && *s)
s++;
if (*s)
{
if (!ft_strchr(s, c))
len = ft_strlen(s);
else
len = ft_strchr(s, c) - s;
r[i] = ft_substr(s, 0, len);
if (!r[i])
return (hmalloc(r));
s = s + len;
i++;
}
}
r[i] = NULL;
return (r);
}
char **ft_split(char const *s, char c)
{
char **r;
int i;
if (!s)
return (0);
r = (char **)malloc((countw(s, c) + 1) * sizeof(char *));
if (!r)
return (0);
i = 0;
r = min (s, c, r, i);
return (r);
}