-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
84 lines (76 loc) · 1.86 KB
/
ft_split.c
File metadata and controls
84 lines (76 loc) · 1.86 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yel-mota <yel-mota@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/09 23:10:15 by yel-mota #+# #+# */
/* Updated: 2025/04/09 23:10:16 by yel-mota ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_contword(const char *str, char c)
{
size_t i;
size_t cont;
i = 0;
cont = 0;
while (str[i] != '\0')
{
if (str[i] == c)
i++;
else
{
cont++;
while (str[i] != c && str[i] != '\0')
i++;
}
}
return (cont);
}
static size_t ft_lenstr(char const *str, char c)
{
size_t i;
i = 0;
while (str[i] != '\0')
{
if (str[i] == c)
return (i);
i++;
}
return (i);
}
char **ft_freetable(char **c, size_t i)
{
while (i--)
free(c[i]);
free(c);
return (NULL);
}
char **ft_split(const char *s, char c)
{
size_t i;
size_t j;
char **table;
i = 0;
j = 0;
if (s == NULL)
return (NULL);
table = malloc(sizeof(char *) * (ft_contword(s, c) + 1));
if (table == NULL)
return (NULL);
while (s[i] != '\0')
{
while (s[i] == c)
i++;
if (s[i] == '\0')
break ;
table[j] = ft_substr(s, i, ft_lenstr(s + i, c));
if (table[j++] == NULL)
return (ft_freetable(table, j));
i += ft_lenstr(s + i, c);
}
table[j] = NULL;
return (table);
}