-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
89 lines (82 loc) · 1.92 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vscabell <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/27 10:49:04 by vscabell #+# #+# */
/* Updated: 2020/01/29 09:10:03 by vscabell ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_elem(const char *s, char c)
{
int i;
int elem;
i = 0;
elem = 0;
while (s[i])
{
if (s[i] != c && s[i] != '\0')
{
elem++;
while (s[i] != c && s[i] != '\0')
i++;
}
else
i++;
}
return (elem);
}
static int *ft_position_elem(const char *s, char c, int elem)
{
int *p;
int i;
int j;
i = 0;
j = 0;
if (!(p = malloc((elem * 2) * sizeof(int))))
return (NULL);
while (s[i])
{
if (s[i] != c && s[i] != '\0')
{
p[j] = i;
i++;
j++;
while (s[i] != c && s[i] != '\0')
i++;
p[j] = i;
j++;
}
else
i++;
}
return (p);
}
char **ft_split(char const *s, char c)
{
int i;
int j;
int elem;
int *posit;
char **arr;
i = 0;
j = 0;
if (!s)
return (NULL);
elem = ft_count_elem(s, c);
posit = ft_position_elem(s, c, elem);
if (!(arr = (char **)malloc((elem + 1) * sizeof(char *))))
return (NULL);
while (j < elem)
{
arr[j] = ft_substr(s, ((unsigned int)posit[i]),
((size_t)(posit[i + 1] - posit[i])));
i = i + 2;
j++;
}
arr[j] = NULL;
return (arr);
}