-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
137 lines (122 loc) · 2.96 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edetoh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/19 10:31:59 by edetoh #+# #+# */
/* Updated: 2024/10/25 11:16:14 by edetoh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* tab_malloc allocates and copies a substring.
* Takes 'str' (source string), 'start' (start index),
* len' (substring length) and 'tab' (string array).
* Returns the substring or NULL on error.
*/
static char *malloc_tab(const char *str, int start, int len)
{
char *sub;
sub = malloc((len + 1) * sizeof(char));
if (!sub)
return (NULL);
ft_strlcpy(sub, (char *)str + start, len + 1);
return (sub);
}
static int count_words(char const *str, char del)
{
int i;
int count;
i = 0;
count = 0;
while (str[i] != '\0')
{
while (str[i] == del)
i++;
if (str[i] != '\0')
count++;
while (str[i] != '\0' && str[i] != del)
i++;
}
return (count);
}
static char **malloc_main_tab(char **tab, char const *str, char del)
{
if (str[0] == '\0')
{
tab = malloc(1 * sizeof(char *));
if (!tab)
return (NULL);
tab[0] = NULL;
return (tab);
}
tab = malloc(((count_words(str, del) + 1) * sizeof(char *)));
if (!tab)
return (NULL);
return (tab);
}
static int fill_tab(char const *str, char del, char **tab)
{
int i;
int n;
int k;
i = 0;
k = 0;
while (str[i] != '\0')
{
n = 0;
while (str[i] == del)
i++;
while (str[i + n] != '\0' && str[i + n] != del)
n++;
if (n != 0)
tab[k++] = malloc_tab(str, i, n);
if ((k > 0 && tab[k - 1] == NULL) || !tab)
return (0);
i = i + n;
}
tab[k] = NULL;
return (1);
}
char **ft_split(char const *s, char c)
{
char **tab;
int i;
if (!s)
return (NULL);
i = 0;
tab = NULL;
tab = malloc_main_tab(tab, s, c);
if (!tab)
return (NULL);
if (fill_tab(s, c, tab) == 0)
{
while (tab[i] != NULL)
free(tab[i++]);
free(tab);
return (NULL);
}
return (tab);
}
// #include <string.h>
// #include <stdio.h>
// #include <unistd.h>
// int main(void)
// {
// char *str = "bonjourzbonjourzbuhuhuzgyguhzohh";
// char del = 'z';
// int i = 0;
// char **res = ft_split(str, del);
// printf("===== RESULT =====\n");
// while (res[i] != NULL)
// {
// printf("Tab DE %i : %s\n", i, res[i]);
// free(res[i]);
// i++;
// }
// printf("==================\n");
// free(res);
// return (1);
// }