-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
104 lines (94 loc) · 2.18 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yanflous <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/28 21:24:51 by yanflous #+# #+# */
/* Updated: 2024/11/08 10:27:28 by yanflous ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t count_word(const char *s, char c)
{
size_t i;
size_t count;
i = 0;
count = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i])
count++;
while (s[i] && s[i] != c)
i++;
}
return (count);
}
static void mem_fill(char *ptr, const char *s, \
size_t length, size_t start)
{
size_t i;
i = 0;
while (i < length)
{
ptr[i] = s[start];
i++;
start++;
}
ptr[i] = '\0';
}
static char *split_words(const char *s, char c, size_t *index)
{
size_t start;
size_t length;
char *s_word;
start = *index;
while (s[start] && s[start] == c)
start++;
*index = start;
length = 0;
while (s[*index] && s[*index] != c)
{
length++;
(*index)++;
}
s_word = malloc(sizeof(char) * (length + 1));
if (!s_word)
return (NULL);
mem_fill(s_word, s, length, start);
return (s_word);
}
static char **mem_free(int i, char **ptr)
{
while (i > 0)
free(ptr[--i]);
free(ptr);
return (NULL);
}
char **ft_split(char const *s, char c)
{
size_t index;
char **ptr;
int c_word;
int i;
if (!s)
return (NULL);
index = 0;
c_word = count_word(s, c);
ptr = malloc(sizeof(char *) * (c_word + 1));
if (!ptr)
return (NULL);
i = 0;
while (i < c_word)
{
ptr[i] = split_words(s, c, &index);
if (!ptr[i])
return (mem_free(i, ptr));
i++;
}
ptr[i] = NULL;
return (ptr);
}