-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
53 lines (48 loc) · 1.49 KB
/
ft_strsplit.c
File metadata and controls
53 lines (48 loc) · 1.49 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: snechaev <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/26 12:36:54 by snechaev #+# #+# */
/* Updated: 2019/02/26 17:41:10 by snechaev ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ft_arr_fill(char **ar, char const *s, char c)
{
char **pa;
char *p;
char *sw;
pa = ar;
p = (char *)s;
sw = 0;
while (*p)
{
if ((*p != c) && ((*(p - 1) == c) || p == s))
sw = p;
else if (sw && *p == c && *(p - 1) != c)
{
*pa = ft_strnew(p - sw);
ft_strncpy(*pa, sw, p - sw);
sw = 0;
pa++;
}
p++;
}
if (sw)
*pa++ = ft_strdup(sw);
*pa = NULL;
}
char **ft_strsplit(char const *s, char c)
{
char **arr;
if (!s)
return (NULL);
arr = (char **)malloc(sizeof(char*) * ft_cnt_words(s, c) + 1);
if (!arr)
return (NULL);
ft_arr_fill(arr, s, c);
return (arr);
}