-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
83 lines (76 loc) · 1.9 KB
/
ft_strsplit.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rnbou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/11 00:19:40 by rnbou #+# #+# */
/* Updated: 2018/10/17 19:51:08 by rnbou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_count(const char *s, char c)
{
size_t nbr;
size_t i;
size_t e;
e = ft_strlen(s);
if (e != 0)
e--;
while (e != 0 && s[e] == c)
e--;
if (e == 0)
return (0);
i = 0;
nbr = 0;
while (i < e)
{
while (s[i] == c && s[i] != '\0')
i++;
while (s[i] != c && s[i] != '\0')
i++;
if (i != e)
nbr++;
}
return (nbr);
}
static size_t *ft_findnext(const char *s, size_t i, char c)
{
size_t j;
size_t *l;
l = (size_t *)malloc(sizeof(size_t) * 2);
while (s[i] == c && s[i] != '\0')
i++;
j = i;
while (s[j] != c && s[j] != '\0')
j++;
if (j != i)
{
l[0] = i;
l[1] = j - i;
}
return (l);
}
char **ft_strsplit(const char *s, char c)
{
size_t len;
char **str;
size_t *i;
size_t j;
size_t i1;
len = ft_count(s, c);
if (!(str = (char **)malloc(sizeof(char *) * (len + 1))))
return (NULL);
i1 = 0;
j = 0;
while (j < len)
{
i = ft_findnext(s, i1, c);
str[j] = ft_strsub(s, ((unsigned int *)i)[0], i[1]);
i1 = i[0] + i[1];
j++;
}
str[j] = NULL;
return (str);
}