-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
101 lines (90 loc) · 1.94 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fgata-va <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/25 12:54:07 by fgata-va #+# #+# */
/* Updated: 2021/03/19 11:11:31 by fgata-va ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_wrds(char const *s, char c)
{
int wrds;
int wrd_fnd;
wrds = 0;
wrd_fnd = 0;
while (*s != '\0')
{
if (*s != c && wrd_fnd == 0)
{
wrds++;
wrd_fnd = 1;
}
else if (*s == c)
wrd_fnd = 0;
s++;
}
return (wrds);
}
int ft_chars(char const *s, char c)
{
int cs;
cs = 0;
while (*s != c && *s != '\0')
{
cs++;
s++;
}
return (cs);
}
void ft_del_matrix(char **matrix)
{
int i;
i = 0;
while (matrix[i])
{
free(matrix[i]);
i++;
}
free(matrix);
*matrix = NULL;
}
void ft_fillstr(int j, char **wrds, char const *s, char c)
{
int i;
i = 0;
while (*s != c && *s)
{
wrds[j][i++] = *s;
s++;
}
}
char **ft_split(char const *s, char c)
{
char **wrds;
int wrd_l;
int j;
if (!s)
return (NULL);
wrds = ft_calloc(ft_wrds(s, c) + 1, sizeof(char *));
j = 0;
while (*s && wrds)
{
while (*s == c)
s++;
wrd_l = ft_chars(s, c);
if (wrd_l)
{
wrds[j] = ft_calloc((wrd_l + 1), sizeof(char));
if (!wrds[j])
ft_del_matrix(wrds);
ft_fillstr(j, wrds, s, c);
s += wrd_l;
}
j++;
}
return (wrds);
}