-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
85 lines (76 loc) · 1.91 KB
/
ft_split.c
File metadata and controls
85 lines (76 loc) · 1.91 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ymassiou <ymassiou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/04 18:47:16 by ymassiou #+# #+# */
/* Updated: 2023/11/14 18:01:08 by ymassiou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *gen_word(const char *s, char c)
{
size_t i;
char *result;
result = NULL;
i = 0;
while (s[i] && s[i] != c)
i++;
result = ft_substr(s, 0, i);
return (result);
}
static void *ft_free(char **to_free, size_t elements)
{
size_t i;
i = 0;
while (i < elements)
{
free(to_free[i]);
i++;
}
free(to_free);
return (NULL);
}
static int words_count(const char *s, char c)
{
size_t i;
size_t count;
i = 0;
count = 0;
while (s[i])
{
if (s[i] != c && (s[i + 1] == 0 || s[i + 1] == c))
count++;
i++;
}
return (count);
}
char **ft_split(char const *s, char c)
{
size_t words;
char **result;
size_t j;
if (s == NULL)
return (NULL);
words = words_count(s, c);
result = NULL;
j = 0;
result = (char **)malloc((words + 1) * sizeof(char *));
if (result == NULL)
return (NULL);
while (*s && words--)
{
while (*s == c && *s)
s++;
result[j] = gen_word(s, c);
if (result[j] == NULL)
return (ft_free(result, j));
j++;
while (*s != c && *s)
s++;
}
result[j] = 0;
return (result);
}