-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
62 lines (57 loc) · 1.56 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ben <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/06/04 13:33:37 by bwebb #+# #+# */
/* Updated: 2020/05/14 14:33:38 by ben ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int wc(char const *s, char c)
{
int i;
int k;
i = 0;
k = 0;
while (s[i])
{
if ((s[i] != c) && (s[i] != '\0'))
{
k++;
while ((s[i] != c) && (s[i + 1] != '\0'))
i++;
}
i++;
}
return (k);
}
char **ft_strsplit(char const *s, char c)
{
int i;
int k;
int j;
char **strs;
if (!s || !c)
return (NULL);
if (!(strs = malloc((wc(s, c) + 1) * sizeof(char *))))
return (NULL);
i = -1;
k = 0;
while (s[++i])
if ((s[i] != c) && (s[i] != '\0'))
{
j = 1;
while ((s[++i] != c) && (s[i] != '\0'))
j++;
if (!(strs[k] = ft_strsub(s, i - j, j)))
return (NULL);
k++;
if (s[i] == '\0')
i--;
}
strs[k] = NULL;
return (strs);
}