-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
87 lines (78 loc) · 1.87 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: earnaud <earnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/10 13:31:58 by earnaud #+# #+# */
/* Updated: 2020/11/17 19:13:04 by earnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **ft_free_neo(char **neo)
{
size_t i;
i = 0;
if (neo)
{
while (neo[i])
free(neo[i++]);
free(neo);
}
return (0);
}
static size_t ft_strllen(char const *s, size_t start, char c)
{
size_t i;
i = 0;
while (s[start] && s[start] != c)
{
i++;
start++;
}
return (i);
}
static size_t ft_nbrword(char const *s, char c)
{
size_t i;
size_t result;
i = 0;
result = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] != c && s[i])
result++;
while (s[i] != c && s[i])
i++;
}
return (result);
}
char **ft_split(char const *s, char c)
{
char **result;
size_t i;
size_t j;
size_t k;
size_t words;
words = ft_nbrword(s, c);
i = 0;
j = 0;
k = 0;
if (!(result = malloc((words + 1) * sizeof(char *))))
return (ft_free_neo(result));
while (i < words)
{
k += j;
while (s[k] == c)
k++;
j = ft_strllen(s, k, c);
if (!(result[i] = ft_substr(s, k, j)))
return (ft_free_neo(result));
i++;
}
result[words] = 0;
return (result);
}