-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
103 lines (93 loc) · 2.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ldel-val <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/30 18:39:55 by ldel-val #+# #+# */
/* Updated: 2024/10/09 12:18:28 by ldel-val ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_count_words(char const *s, char c)
{
size_t word_count;
size_t i;
word_count = 0;
i = 0;
while (s[i])
{
if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0'))
word_count++;
i ++;
}
return (word_count);
}
static char *ft_get_word(char const *s, char c, size_t n)
{
size_t current_word;
size_t word_len;
size_t i;
i = 0;
current_word = 0;
if (s[i] != c)
current_word = 1;
while (s[i] && current_word < n)
{
if (s[i] == c && s[i + 1] != c)
current_word ++;
i ++;
}
word_len = 0;
while (s[i + word_len] != c && s[i + word_len])
word_len ++;
return (ft_substr(s, i, word_len));
}
static char **ft_free_array(char **array, size_t n)
{
size_t i;
i = 0;
while (i < n)
{
free(array[i]);
i ++;
}
free(array);
return (NULL);
}
char **ft_split(char const *s, char c)
{
long word_count;
long i;
char **split;
word_count = ft_count_words(s, c);
split = malloc(sizeof(char *) * (word_count + 1));
if (!split)
return (NULL);
i = 0;
while (i < word_count)
{
split[i] = ft_get_word(s, c, i + 1);
if (!split[i])
return (ft_free_array(split, i));
i ++;
}
split[i] = NULL;
return (split);
}
/*
int main(void)
{
char s[] = "lorem ipsum dolor sit amet, consectetur adipiscing elit.";
char c = ' ';
char **split;
//printf("%ld\n", ft_count_words(s, c));
//printf("%s\n", ft_get_word(s, c, 1));
split = ft_split(s, c);
while (*split)
{
printf("%s\n", *split);
split ++;
}
}*/