-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
83 lines (75 loc) · 1.85 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nlyamani <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 17:49:27 by nlyamani #+# #+# */
/* Updated: 2024/10/05 18:09:35 by nlyamani ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_word_count(const char *s, char c)
{
int w_c;
int w_in;
w_c = 0;
w_in = 0;
while (*s)
{
if (*s != c && w_in == 0)
{
w_in = 1;
w_c++;
}
else if (*s == c)
w_in = 0;
s++;
}
return (w_c);
}
static void ft_free(char **words, int words_i)
{
while (words_i-- > 0)
free(words[words_i]);
free(words);
}
static int find_word_end(const char *s, char c)
{
int w_ed;
w_ed = 0;
while (s[w_ed])
{
if (s[w_ed] == c)
return (w_ed);
w_ed++;
}
return (w_ed);
}
char **ft_split(const char *s, char c)
{
char **words;
int words_i;
words_i = 0;
if (!s)
return (NULL);
words = malloc((ft_word_count(s, c) + 1) * sizeof(char *));
if (!words)
return (NULL);
while (*s)
{
while (*s == c)
s++;
if (*s)
{
words[words_i] = ft_substr(s, 0, find_word_end(s, c));
if (!words[words_i])
return (ft_free(words, words_i), NULL);
words_i++;
s += find_word_end(s, c);
}
}
words[words_i] = NULL;
return (words);
}