-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
207 lines (197 loc) · 5.02 KB
/
Copy pathft_split.c
File metadata and controls
207 lines (197 loc) · 5.02 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zakchouc <zakchouc@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 08:27:26 by zakchouc #+# #+# */
/* Updated: 2024/12/03 00:25:11 by zakchouc ### ########.fr */
/* */
/* ************************************************************************** */
/**
* @file ft_split.c
* @author Ziyad A. Dev (zakchouc@student.42.fr)
* @brief Allocates (with malloc(3)) and returns an array of strings
* obtained by splitting ’s’ using the character ’c’ as a delimiter.
* The array must end with a NULL pointer.
* Allowed funct. : malloc, free
* @param s : The string to be split
* @param c : The delimiter character
* @return : The array of new strings resulting from the split.
* NULL if the allocation fails.
* @version 0.1
* @date 2023-11-27
*
* @copyright Copyright (c) 2023
*
*/
#include "libft.h"
static unsigned int ft_count_elements(const char *s, const char c)
{
unsigned int i;
unsigned int elements;
i = 0;
elements = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i])
elements++;
while (s[i] && (s[i] != c))
i++;
}
return (elements);
}
static char *ft_strndup_split(const char *s, size_t n)
{
size_t i;
char *str_dup;
i = 0;
str_dup = malloc(sizeof(char) * (n + 1));
if (!str_dup)
return (NULL);
while (s[i] && (i < n))
{
str_dup[i] = s[i];
i++;
}
str_dup[i] = 0;
return (str_dup);
}
void ft_free_arr(char *arr[], int nb_elmt)
{
while (nb_elmt >= 0)
free(arr[nb_elmt--]);
free(arr);
}
char **ft_split(char const *s, char c)
{
int elmt;
unsigned int i;
unsigned int j;
int k;
char **arr_of_elmts;
elmt = 0;
i = 0;
k = ft_count_elements(s, c);
arr_of_elmts = ft_calloc(k + 1, sizeof(char *));
if (!arr_of_elmts)
return (0);
while (elmt < k)
{
while (s[i] && s[i] == c)
i++;
j = i;
while (s[i] && s[i] != c)
i++;
arr_of_elmts[elmt] = ft_strndup_split((&s[j]), (i - j));
if (!arr_of_elmts[elmt])
return (ft_free_arr(arr_of_elmts, elmt), NULL);
elmt++;
}
return (arr_of_elmts[elmt] = 0, arr_of_elmts);
}
// #include "ft_calloc.c"
// #include "ft_bzero.c"
// int main(void)
// {
// const char to_be_splitted_1[] = "";
// const char to_be_splitted_2[] = "salut,toi";
// const char to_be_splitted_3[] = ",";
// const char to_be_splitted_4[] = "\0aa\0bbb";
// const char to_be_splitted_5[] = "salut";
// const char to_be_splitted_6[] =
// " split this for me ! ";
// const char to_be_splitted_7[] = " ";
// char **splitted;
// int i;
// int elmts;
//
// i = 0;
// elmts = ft_count_elements(to_be_splitted_1, ',');
//
// splitted = ft_split(to_be_splitted_1, ',');
// printf("\n--1--\nelements : %d\n", elmts);
// while (i <= elmts)
// {
// printf("str %d : %s\n", i, splitted[i]);
// i++;
// }
// free(splitted);
//
// i = 0;
// elmts = ft_count_elements(to_be_splitted_2, ',');
//
// splitted = ft_split(to_be_splitted_2, ',');
// printf("\n--2--\nelements : %d\n", elmts);
// while (i <= elmts)
// {
// printf("str %d : %s\n", i, splitted[i]);
// i++;
// }
// free(splitted);
//
// i = 0;
// elmts = ft_count_elements(to_be_splitted_3, ',');
//
// splitted = ft_split(to_be_splitted_3, ',');
// printf("\n--3--\nelements : %d\n", elmts);
// while (i <= elmts)
// {
// printf("str %d : %s\n", i, splitted[i]);
// i++;
// }
// free(splitted);
//
// i = 0;
// elmts = ft_count_elements(to_be_splitted_4, '\0');
//
// splitted = ft_split(to_be_splitted_4, '\0');
// printf("\n--4--\nelements : %d\n", elmts);
// while (i <= elmts)
// {
// printf("str %d : %s\n", i, splitted[i]);
// i++;
// }
// free(splitted);
//
// i = 0;
// elmts = ft_count_elements(to_be_splitted_5, '\0');
//
// splitted = ft_split(to_be_splitted_5, '\0');
// printf("\n--5--\nelements : %d\n", elmts);
// while (i <= elmts)
// {
// printf("str %d : %s\n", i, splitted[i]);
// i++;
// }
// free(splitted);
//
// i = 0;
// elmts = ft_count_elements(to_be_splitted_6, ' ');
//
// splitted = ft_split(to_be_splitted_6, ' ');
// printf("\n--6--\nelements : %d\n", elmts);
// while (i <= elmts)
// {
// printf("str %d : %s\n", i, splitted[i]);
// i++;
// }
// free(splitted);
//
// i = 0;
// elmts = ft_count_elements(to_be_splitted_7, ' ');
//
// splitted = ft_split(to_be_splitted_7, ' ');
// printf("\n--7--\nelements : %d\n", elmts);
// while (i <= elmts)
// {
// printf("str %d : %s\n", i, splitted[i]);
// i++;
// }
// free(splitted);
//
// return (0);
// }