-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
107 lines (97 loc) · 2.05 KB
/
Copy pathft_split.c
File metadata and controls
107 lines (97 loc) · 2.05 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
95
96
97
98
99
100
101
102
103
104
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: astoll <astoll@student.42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:04:31 by astoll #+# #+# */
/* Updated: 2023/11/07 22:11:22 by astoll ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_count(char const *s, char c)
{
size_t count;
size_t i;
count = 0;
i = 0;
while (s[i] != '\0')
{
if (s[i] != c)
{
count++;
while (s[i] != '\0' && s[i] != c)
{
i++;
}
if (s[i] == '\0')
{
return (count);
}
}
i++;
}
return (count);
}
static size_t ft_len(char const *s, char c)
{
size_t i;
i = 0;
while (s[i] != '\0' && s[i] != c)
{
i++;
}
return (i);
}
static void ft_free(size_t i, char **array)
{
while (i > 0)
{
i--;
free(array[i]);
}
free(array);
}
static char **split(char const *s, char c, char **array, size_t count)
{
size_t i;
size_t j;
i = 0;
j = 0;
while (i < count)
{
while (s[j] != '\0' && s[j] == c)
{
j++;
}
array[i] = ft_substr(s, j, ft_len(&s[j], c));
if (!(array[i]))
{
ft_free(i, array);
return (NULL);
}
while (s[j] != '\0' && s[j] != c)
{
j++;
}
i++;
}
array[i] = NULL;
return (array);
}
char **ft_split(char const *s, char c)
{
char **array;
if (!s)
{
return (NULL);
}
array = (char **)malloc(sizeof(char *) * (ft_count(s, c) + 1));
if (!array)
{
return (NULL);
}
array = split(s, c, array, ft_count(s, c));
return (array);
}