-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
72 lines (65 loc) · 1.71 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yjouaoud <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/08 20:40:35 by yjouaoud #+# #+# */
/* Updated: 2019/04/13 15:26:37 by yjouaoud ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_wc(char const *s, char c)
{
int i;
int wc;
i = 0;
wc = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] != c && s[i] != '\0')
wc++;
while (s[i] != c && s[i] != '\0')
i++;
}
return (wc);
}
static int ft_cc(const char *s, char c)
{
int cc;
int i;
cc = 0;
i = 0;
while (s[i] == c)
i++;
while (s[i] != c && s[i++])
cc++;
return (cc);
}
char **ft_strsplit(char const *s, char c)
{
char **split;
int i;
int j;
int k;
i = -1;
j = 0;
if (!s || !(split = (char **)malloc(sizeof(*split) * (ft_wc(s, c) + 1))))
return (NULL);
while (++i < ft_wc(s, c))
{
if (!(split[i] = (char*)malloc(ft_cc(&s[j], c) + 1)))
return (NULL);
k = 0;
while (s[j] == c)
j++;
while (s[j] != c && s[j])
split[i][k++] = s[j++];
split[i][k] = '\0';
}
split[i] = 0;
return (split);
}