-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
42 lines (39 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rgaia <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/04 13:10:17 by rgaia #+# #+# */
/* Updated: 2017/10/09 23:49:35 by rgaia ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char **ft_strsplit(char const *s, char c)
{
size_t i;
size_t j;
size_t k;
char **split;
i = 0;
k = 0;
if (!s || !(split = (char **)malloc(sizeof(char *) *
(ft_strnword(s, c) + 1))))
return (NULL);
while (i < (size_t)ft_strnword(s, c))
{
if (!(split[i] = (char *)malloc(sizeof(char) *
(ft_strlen(&s[k] + 1)))))
return (NULL);
j = 0;
while (s[k] == c)
k++;
while (s[k] && s[k] != c)
split[i][j++] = s[k++];
split[i][j] = '\0';
i++;
}
split[i] = NULL;
return (split);
}