-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
126 lines (115 loc) · 2.42 KB
/
ft_split.c
File metadata and controls
126 lines (115 loc) · 2.42 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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thlibers <thlibers@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/10/16 09:26:53 by thlibers #+# #+# */
/* Updated: 2025/10/20 15:19:44 by thlibers ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t count_word(char const *s, char sep)
{
size_t i;
size_t count;
i = 0;
count = 0;
while (s[i] && s[i] == sep)
i++;
while (s[i])
{
while (s[i] && s[i] != sep)
i++;
count++;
while (s[i] && s[i] == sep)
i++;
}
return (count);
}
static char *word_dup(const char *s, char sep)
{
int i;
char *str;
i = 0;
if (s == NULL)
return (NULL);
while (s[i] != sep && s[i])
i++;
str = malloc(sizeof(char) * (i + 1));
if (str == NULL)
return (NULL);
i = 0;
while (s[i] && s[i] != sep)
{
str[i] = s[i];
i++;
}
str[i] = '\0';
return (str);
}
static void free_tab(char **s)
{
size_t i;
i = 0;
while (s[i])
{
free(s[i]);
i++;
}
free(s);
}
char **ft_split(char const *s, char c)
{
char **tab;
size_t i;
size_t j;
tab = malloc(sizeof(char *) * (count_word(s, c) + 1));
if (tab == NULL)
return (NULL);
i = 0;
j = 0;
while (s[i])
{
while (s[i] == c && s[i])
i++;
if (!s[i])
break ;
tab[j] = word_dup(&(s[i]), c);
if (tab[j] == NULL)
return (free_tab(tab), NULL);
j++;
while (s[i] != c && s[i])
i++;
}
tab[j] = NULL;
return (tab);
}
// int main()
// {
// size_t j;
// j = 0;
// char str[] = "salut/f/wooooooooooow/";
// // char str[] = "/////////";
// char sep = '/';
// // char sep = '\0';
// char **s;
// s = ft_split(str, sep);
// while (s[j])
// {
// printf ("%s\n", s[j]);
// j++;
// }
// // size_t i;
// // i = 0;
// // while(s[i])
// // {
// // free(s[i]);
// // i++;
// // }
// // free(s[i]);
// free_tab(s);
// // free(s);
// return(0);
// }