-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
40 lines (37 loc) · 1.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mtan <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/02/21 17:41:02 by mtan #+# #+# */
/* Updated: 2018/02/21 17:41:10 by mtan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char **ft_strsplit(char const *s, char c)
{
size_t i;
size_t j;
size_t k;
char **w;
i = 0;
k = 0;
if (!s || !(w = (char**)malloc(sizeof(char*) * (ft_wordcount(s, c) + 1))))
return (NULL);
while (i < ft_wordcount(s, c))
{
if (!(w[i] = (char*)malloc(sizeof(char) * (ft_wordlen(&s[k], c) + 1))))
return (NULL);
j = 0;
while (s[k] == c)
k++;
while (s[k] != c && s[k])
w[i][j++] = s[k++];
w[i][j] = '\0';
i++;
}
w[i] = NULL;
return (w);
}