-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
81 lines (72 loc) · 1.9 KB
/
Copy pathft_split.c
File metadata and controls
81 lines (72 loc) · 1.9 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ftonita <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/02 00:15:47 by ftonita #+# #+# */
/* Updated: 2021/11/02 00:15:49 by ftonita ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_word(const char *s, int c)
{
int k;
int i;
k = 0;
i = 0;
while (s[i])
{
if (s[i] != c && (s[i + 1] == c || s[i + 1] == '\0'))
k++;
i++;
}
return (k);
}
static int ft_wlen(const char *s, char c)
{
int b;
b = 0;
while (s[b] != c && s[b])
b++;
return (b);
}
static char **ft_msplit(char const *s, char c, int i, char **a)
{
size_t j;
j = 0;
while (s[i])
{
a[j] = (char *)malloc((ft_wlen(s + i, c) + 1) * sizeof(char));
if (!a[j])
{
while (*(a + j))
free(a[j--]);
free(a);
return (NULL);
}
ft_bzero(a[j], ft_wlen(s + i, c) + 1);
ft_strlcat(a[j], s + i, (size_t)ft_wlen(s + i, c) + 1);
j++;
i += ft_wlen(s + i, c);
while (s[i] && s[i] == c && c != '\0')
i++;
}
a[j] = (0);
return (a);
}
char **ft_split(char const *s, char c)
{
char **a;
int i;
if (!s)
return (NULL);
a = (char **)malloc(sizeof(char *) * (ft_count_word(s, c) + 1));
if (!a)
return (0);
i = 0;
while (s[i] && s[i] == c && c != '\0')
i++;
return (ft_msplit(s, c, i, a));
}