-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
72 lines (65 loc) · 1.78 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
68
69
70
71
72
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gahubaul <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/11/30 17:09:32 by gahubaul #+# #+# */
/* Updated: 2015/11/30 17:09:32 by gahubaul ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_cnt_parts(const char *s, char c)
{
int cnt;
int in_substring;
in_substring = 0;
cnt = 0;
while (*s != '\0')
{
if (in_substring == 1 && *s == c)
in_substring = 0;
if (in_substring == 0 && *s != c)
{
in_substring = 1;
cnt++;
}
s++;
}
return (cnt);
}
static int ft_wlen(const char *s, char c)
{
int len;
len = 0;
while (*s != c && *s != '\0')
{
len++;
s++;
}
return (len);
}
char **ft_strsplit(char const *s, char c)
{
char **t;
int nb_word;
int index;
index = 0;
nb_word = ft_cnt_parts((const char *)s, c);
t = (char **)malloc(sizeof(*t) * (ft_cnt_parts((const char *)s, c) + 1));
if (!t)
return (NULL);
while (nb_word--)
{
while (*s == c && *s != '\0')
s++;
t[index] = ft_strsub((const char *)s, 0, ft_wlen((const char *)s, c));
if (!t[index])
return (NULL);
s = s + ft_wlen(s, c);
index++;
}
t[index] = NULL;
return (t);
}