-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
81 lines (74 loc) · 1.76 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ybouzgao <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/10 14:18:11 by ybouzgao #+# #+# */
/* Updated: 2017/11/14 17:38:52 by ybouzgao ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int nbw(char *s, char c)
{
int i;
int sym;
int count;
i = 0;
sym = 0;
count = 0;
if (!s)
return (0);
while (s[i])
{
while (s[i] != c && s[i])
{
sym = 1;
i++;
}
while (s[i] == c && s[i])
{
count += sym;
sym = 0;
i++;
}
}
return (count + 2);
}
static int nbc(char *s, int i, char c)
{
int count;
count = 0;
while (s[i] != c && s[i])
{
count++;
i++;
}
return (count + 1);
}
char **ft_strsplit(const char *s, char c)
{
char **res;
int i;
int j;
int k;
i = 0;
j = 0;
s = ft_strtrimc(s, c);
if (!s || !(res = malloc(sizeof(char *) * nbw((char *)s, c))))
return (NULL);
while (s[i])
{
k = 0;
if (!(res[j] = malloc(sizeof(c) * nbc((char*)s, i, c))))
return (0);
while (s[i] != c && s[i])
res[j][k++] = s[i++];
while (s[i] == c && s[i])
i++;
res[j++][k] = '\0';
}
res[j] = NULL;
return (res);
}