-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsplit.c
executable file
·42 lines (39 loc) · 1.44 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: istalevs <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/16 20:13:04 by istalevs #+# #+# */
/* Updated: 2017/12/15 19:06:13 by istalevs ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char **ft_strsplit(char const *s, char c)
{
const char *tmp;
char **demo;
int words;
int count;
if (s == NULL)
return (NULL);
count = 0;
words = ft_splitter(s, c) + 1;
if ((demo = (char**)malloc(words * sizeof(char*))) == NULL)
return (NULL);
(void)ft_memset(demo, '\0', words-- * sizeof(char*));
while (count < words)
{
while (*s == c)
s++;
tmp = s;
while (*tmp != c && *tmp)
tmp++;
if (*tmp == c || *tmp == '\0')
if ((demo[count++] = ft_strndup(s, (tmp++ - s))) == NULL)
return (NULL);
s = tmp;
}
return (demo);
}