-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
100 lines (91 loc) · 2.21 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: wivieira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 11:09:24 by wivieira #+# #+# */
/* Updated: 2023/11/03 11:53:17 by wivieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_srchparams(char const *s, char c)
{
size_t srch;
size_t i;
srch = 0;
i = 0;
if (*s == '\0')
return (0);
while (s[i] != '\0' && s[i] == c)
i++;
while (s[i])
{
srch++;
while (s[i] != c && s[i])
i++;
while (s[i] == c && s[i])
i++;
}
return (srch);
}
void ft_free_memory(char **matrix)
{
int i;
i = 0;
while (matrix[i])
{
free(matrix[i]);
i++;
}
free(matrix);
}
char *ft_extract_word(char const **s, char c)
{
const char *start;
start = *s;
while (**s != c && **s)
(*s)++;
return (ft_substr(start, 0, *s - start));
}
char **ft_split(char const *s, char c)
{
char **arrspt;
const char *start;
size_t i;
arrspt = (char **)malloc((ft_srchparams(s, c) + 1) * sizeof(char *));
if (arrspt == NULL)
return (NULL);
arrspt[ft_srchparams(s, c)] = NULL;
i = -1;
while (*s)
{
while (*s == c && *s)
s++;
if (*s)
{
start = s;
arrspt[++i] = ft_extract_word(&s, c);
if (arrspt[i] == NULL)
{
ft_free_memory (arrspt);
return (NULL);
}
}
}
return (arrspt);
}
// int main(void)
// {
// char **ref;
// char c = 32;
// int i;
// ref = ft_split("nonempty", '\0');
// i = 0;
// while(ref[i])
// {
// printf("[%d] %s \n", i, ref[i]);
// i++;
// }
// }