-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
88 lines (81 loc) · 1.83 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
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thifranc <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/02/16 11:14:37 by thifranc #+# #+# */
/* Updated: 2016/02/20 17:56:42 by thifranc ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_wrd_ct(char const *s, char c)
{
int i;
int ct;
i = 0;
ct = 0;
while (s[i])
{
while (s[i] && s[i] == c)
i++;
if (s[i])
ct++;
while (s[i] && s[i] != c)
i++;
}
return (ct);
}
static char **ft_get_let(char const *s, char c, char **out)
{
int i;
int x;
int y;
i = 0;
y = 0;
while (s[i])
{
while (s[i] && s[i] == c)
i++;
x = 0;
while (s[i] && s[i] != c)
{
out[y][x] = s[i];
i++;
x++;
}
out[y][x] = '\0';
y++;
}
if (s[i - 1] == c)
y--;
out[y] = NULL;
return (out);
}
char **ft_strsplit(char const *s, char c)
{
int x;
int y;
char **out;
int i;
y = 0;
i = 0;
if (!(out = (char**)malloc(sizeof(char*) * (ft_wrd_ct(s, c) + 1))))
return (NULL);
while (s[i])
{
while (s[i] && s[i] == c)
i++;
x = 0;
while (s[i] && s[i] != c)
{
i++;
x++;
}
if (!(out[y] = (char*)malloc(sizeof(char) * (x + 1))))
return (NULL);
y++;
}
return (ft_get_let(s, c, out));
}