-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
76 lines (69 loc) · 1.74 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dskrypny <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/18 15:06:01 by dskrypny #+# #+# */
/* Updated: 2017/11/19 17:28:40 by dskrypny ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count(char const *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (s[i] == c)
i++;
while (s[i])
{
while (s[i] != c && s[i])
i++;
count++;
while (s[i] == c && s[i])
i++;
}
return (count);
}
static int ft_fill(char *res, const char *s, int i, int size)
{
int j;
j = 0;
while (j < size)
{
res[j] = s[i + j];
j++;
}
res[j] = 0;
return (1);
}
char **ft_strsplit(char const *s, char c)
{
char **res;
int i;
int k;
int size;
if (!(res = (char **)malloc(sizeof(char *) * (ft_count(s, c) + 1))))
return (NULL);
k = 0;
i = 0;
while (k < ft_count(s, c))
{
if (s[i] == c)
i++;
else
{
size = 0;
while (s[i + size] != c && s[i + size])
size++;
res[k] = (char *)malloc(sizeof(char) * (size + 1));
k += ft_fill(res[k], s, i, size);
i += size;
}
}
res[k] = 0;
return (res);
}