-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
89 lines (80 loc) · 1.88 KB
/
ft_strsplit.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jsprouts <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/24 23:25:23 by jsprouts #+# #+# */
/* Updated: 2019/09/25 02:48:09 by jsprouts ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_nb(char const *s, char c)
{
int i;
int j;
int nb;
i = 0;
j = 0;
nb = 0;
while (s[i] != 0)
{
if (j == 0 && s[i] != c)
{
j = 1;
nb++;
}
if (j == 1 && s[i] == c)
j = 0;
i++;
}
return (nb);
}
static int ft_count_ln(char const *s, char c)
{
int i;
i = 0;
while (s[i] != c)
i++;
return (i);
}
static int ft_addition(char **str, char const *s, char c, int nb)
{
int i;
int j;
i = 0;
while (i < nb)
{
j = 0;
while (*s == c && *s != 0)
s++;
if (!(str[i] = (char*)malloc(sizeof(char) * (ft_count_ln(s, c) + 1))))
return (-1);
while (*s != c)
{
str[i][j++] = *s;
s++;
}
str[i++][j] = 0;
}
str[i] = NULL;
return (0);
}
char **ft_strsplit(char const *s, char c)
{
char **str;
int nb;
if (!s)
return (NULL);
nb = ft_count_nb(s, c);
if (!(str = (char**)malloc(sizeof(char*) * (nb + 1))))
return (NULL);
if (ft_addition(str, s, c, nb) == -1)
{
while (nb >= 0)
free(str[nb--]);
free(str);
}
return (str);
}