-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
214 lines (177 loc) · 4.14 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
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
208
209
210
211
212
213
214
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: musenov <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/06 21:34:05 by musenov #+# #+# */
/* Updated: 2022/12/08 15:24:51 by musenov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_word_count(char const *s, char c);
static int ft_word_len(char const *s, char c, int i);
char **ft_splits(char const *s, char c, int *word_count, char **strings);
char **ft_split(char const *s, char c)
{
int word_count;
char **strings;
if (!s)
return (0);
word_count = ft_word_count(s, c);
strings = (char **)malloc((word_count + 1) * sizeof(char *));
if (!strings)
return (0);
return (ft_splits(s, c, &word_count, strings));
}
char **ft_splits(char const *s, char c, int *word_count, char **strings)
{
int i;
int j;
int word_len;
i = 0;
j = 0;
while (i < *word_count)
{
while (*(s + j) == c)
j++;
word_len = ft_word_len(s, c, j);
*(strings + i) = ft_substr(s, j, word_len);
if (!*(strings + i))
{
while (i-- > 0)
free(*(strings + i));
free(strings);
return (0);
}
j = j + word_len;
i++;
}
*(strings + i) = 0;
return (strings);
}
static int ft_word_count(char const *s, char c)
{
int i;
int word_count;
i = 0;
word_count = 0;
while (s && *(s + i) != '\0')
{
if (*(s + i) == c)
i++;
else
{
word_count++;
while (*(s + i) != '\0' && *(s + i) != c)
i++;
}
}
return (word_count);
}
static int ft_word_len(char const *s, char c, int j)
{
int start;
start = j;
while (*(s + j) != 0 && *(s + j) != c)
j++;
return (j - start);
}
/*
ft_split:
PARAMETERS
s: The string to be split.
c: The delimiter character.
RETURN VALUE
The array of new strings resulting from the split. NULL if
the allocation fails.
DESCRIPTION
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.
QUESTIONS
-/-
ANSWER
-/-
COMPARE
-/-
ALTERNATIVE SOLUTION
-/-
EXPLANATION
char **ft_split(char const *s, char c)
{
int word_count;
char **strings;
if (!s)
return (0);
word_count = ft_word_count(s, c);
strings = (char **)malloc((word_count + 1) * sizeof(char *));
if (!strings)
return (0);
return (ft_splits(s, c, &word_count, strings));
}
// this function must return a NULL-pointer if ft_substr fails as
// we have in it a line of code which returns '0' [return (0)]
char **ft_splits(char const *s, char c, int *word_count, char **strings)
{
int i;
int j;
int word_len;
i = 0;
j = 0;
while (i < *word_count)
{
while (*(s + j) == c)
j++;
word_len = ft_word_len(s, c, j);
*(strings + i) = ft_substr(s, j, word_len);
if (!*(strings + i))
// There is a malloc() functin in ft_substr(). If substr function fails
// we must free the variable strings, first by freeing branches then by freeing
// the variable strings itself
{
while (i-- > 0)
free(*(strings + i));
free(strings);
return (0);
}
j = j + word_len;
i++;
}
// Works only with NULL or 0 and not '\0', why?
*(strings + i) = 0;
return (strings);
}
static int ft_word_count(char const *s, char c)
{
int i;
int word_count;
i = 0;
word_count = 0;
while (s && *(s + i) != '\0')
{
if (*(s + i) == c)
i++;
else
{
word_count++;
while (*(s + i) != '\0' && *(s + i) != c)
i++;
}
}
return (word_count);
}
static int ft_word_len(char const *s, char c, int j)
{
int start;
start = j;
while (*(s + j) != 0 && *(s + j) != c)
j++;
return (j - start);
}
REMARK
-/-
REMARK
-/-
*/