-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
67 lines (62 loc) · 1.65 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
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmoska <tmoska@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/18 09:18:00 by tmoska #+# #+# */
/* Updated: 2017/02/19 23:05:02 by tmoska ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static int word_count(char const *s, char c)
{
int i;
int wc;
int found;
i = 0;
wc = 0;
found = 0;
while (s[i])
{
if (s[i] == c)
found = 0;
else
{
if (found == 0)
wc++;
found = 1;
}
i++;
}
return (wc);
}
char **ft_strsplit(char const *s, char c)
{
char **table;
int size;
int i;
int substr_len;
if (!s)
return (0);
size = word_count(s, c);
substr_len = 0;
i = 0;
if (!(table = (char **)malloc((sizeof(char *) * size + 1))))
return (NULL);
while (size--)
{
while (*s == c && *s != '\0')
s++;
substr_len = ft_strlen_until(s, c);
table[i] = ft_strncpy(ft_strnew(substr_len), s, substr_len);
if (!table[i])
return (NULL);
s += substr_len;
i++;
}
table[i] = NULL;
return (table);
}