-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
121 lines (111 loc) · 2.46 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: edebi <edebi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/09/14 19:45:54 by edebi #+# #+# */
/* Updated: 2020/11/08 18:26:29 by edebi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **alloc_ptr(char const *s, char c)
{
size_t i;
size_t k;
size_t j;
char **res;
j = 0;
k = 0;
i = 0;
while (s[i])
{
if (s[i] == c && i > 0 && s[i - 1] != c)
k++;
i++;
}
if (s[i] == '\0' && i > 0 && s[i - 1] != c)
k++;
res = (char **)malloc(sizeof(char*) * (k + 1));
if (res == NULL)
return ((char **)0);
res[k] = NULL;
return (res);
}
static char **mem_free(char **str, int i)
{
while (i >= 0)
{
free(str[i]);
i--;
}
free(str);
return ((char **)0);
}
static char **alloc_str(char **res, char const *s, char c)
{
int i;
int k;
int j;
j = 0;
k = 0;
i = 0;
while (s[i])
{
if ((s[i] == c) && i > 0 && s[i - 1] != c)
{
if ((res[k] = malloc(j + 1)) == NULL)
return (mem_free(res, k - 1));
j = 0;
k++;
}
else if (s[i] != c)
j++;
i++;
}
if (s[i] == '\0' && i > 0 && s[i - 1] != c)
if ((res[k] = malloc(j + 1)) == NULL)
return (mem_free(res, k - 1));
return (res);
}
static char **get_result(char **res, char const *s, char c)
{
int i;
int k;
int j;
j = 0;
k = 0;
i = 0;
while (s[i] && res[k] != NULL)
{
if (s[i] == c && i != 0 && s[i - 1] != c)
{
res[k][j] = '\0';
j = 0;
k++;
}
else if (s[i] != c)
{
res[k][j] = s[i];
j++;
}
i++;
if (s[i] == '\0' && s[i - 1] != c)
res[k][j] = '\0';
}
return (res);
}
char **ft_split(char const *s, char c)
{
char **res;
if (s == NULL)
return ((char **)0);
if ((res = alloc_ptr(s, c)) == NULL)
return (res);
res = alloc_str(res, s, c);
if (res == NULL)
return (res);
res = get_result(res, s, c);
return (res);
}