-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
84 lines (75 loc) · 1.47 KB
/
ft_split.c
File metadata and controls
84 lines (75 loc) · 1.47 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
#include "libft.h"
static int ft_getnbstr(char const *s, char c);
static int ft_settabstr(char const *s, char c, char **tab_str, int *i);
static int ft_getstrlen(char const *s, char c, int j);
char **ft_split(char const *s, char c)
{
char **tab_str;
int nb_str;
int params[2];
params[0] = 0;
params[1] = 0;
nb_str = ft_getnbstr(s, c);
tab_str = malloc(sizeof(char *) * (nb_str + 1));
if (tab_str == NULL)
return (NULL);
while (params[0] < nb_str)
{
if (ft_settabstr(s, c, tab_str, params))
return (NULL);
params[0]++;
}
tab_str[params[0]] = NULL;
return (tab_str);
}
static int ft_getnbstr(char const *s, char c)
{
int i;
int nb;
i = 0;
if (s[0] == '\0')
return (0);
while (s[i] == c)
i++;
if (s[i] == '\0')
return (0);
nb = 1;
while (s[i])
{
if (s[i] == c && s[i + 1] != '\0' && s[i + 1] != c)
nb++;
i++;
}
return (nb);
}
static int ft_getstrlen(char const *s, char c, int j)
{
int i;
i = 0;
while (s[i + j] != c && s[i + j] != '\0')
i++;
return (i);
}
static int ft_settabstr(char const *s, char c, char **tab_str, int *params)
{
int k;
k = 0;
while (s[params[1]] == c)
params[1]++;
tab_str[params[0]] = malloc(ft_getstrlen(s, c, params[1]) + 1);
if (tab_str[params[0]] == NULL)
{
while (params[0]-- > 0)
free(tab_str[params[0]]);
free(tab_str);
return (1);
}
while (s[params[1]] != c && s[params[1]] != '\0')
{
tab_str[params[0]][k] = s[params[1]];
params[1]++;
k++;
}
tab_str[params[0]][k] = '\0';
return (0);
}