-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
127 lines (116 loc) · 2.52 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
122
123
124
125
126
127
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cclock <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/02 03:55:43 by cclock #+# #+# */
/* Updated: 2020/11/19 23:47:20 by cclock ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count(char const *s, char c)
{
int cs;
int start;
int end;
start = 0;
cs = 0;
end = ft_strlen(s) - 1;
while (s[start] == c && s[start] != 0)
start++;
while (s[end] == c)
end--;
while (start <= end)
{
if (s[start] == c && s[start + 1] != c)
cs++;
start++;
}
return (cs + 1);
}
static char **vydel(char **str, char const *s, char c, int gc)
{
int i;
int j;
int cs;
cs = 0;
i = 0;
j = 0;
while (gc-- != 0)
{
while (s[i] == c && s[i] != 0)
++i;
while (s[i] != c && s[i++] != 0)
++j;
str[cs] = (char *)malloc(sizeof(char) * (j + 1));
if (((j = 0) == 0) && !str[cs])
{
while (str[cs] && cs >= 0)
{
while (cs >= 0)
free(str[cs--]);
}
}
++cs;
}
return (str);
}
static char **zapol(char **str, char const *s, char c, int gc)
{
int i;
int j;
int cs;
cs = 0;
i = 0;
j = 0;
while (gc-- != 0)
{
while (s[i] == c && s[i] != 0)
i++;
while (s[i] != c && s[i] != 0)
str[cs][j++] = s[i++];
str[cs][j] = '\0';
++cs;
j = 0;
}
return (str);
}
static char **check_null(char const *s, char c)
{
char **str;
int i;
i = 0;
while (s[i] != 0)
{
if (s[i] != c)
return (NULL);
i++;
}
str = (char **)malloc(sizeof(char *) * 1);
str[0] = NULL;
return (str);
}
char **ft_split(char const *s, char c)
{
char **str;
int gc;
if (!s)
return (NULL);
if ((str = check_null(s, c)) != NULL)
{
return (str);
}
gc = count(s, c);
if ((str = (char **)malloc(sizeof(char *) * (gc + 1))) == NULL)
return (NULL);
str[gc] = NULL;
str = vydel(str, s, c, gc);
if (!str)
{
free(str);
return (NULL);
}
return (zapol(str, s, c, gc));
}