-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
72 lines (65 loc) · 1.73 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: rabougue <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2015/12/11 13:39:24 by rabougue #+# #+# */
/* Updated: 2016/08/06 22:09:36 by rabougue ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/libft.h"
static void ft_split_count(char const *s, char c, int *j)
{
int i;
i = 0;
while (s[i] != '\0')
{
while (s[i] == c)
i++;
if (s[i] != c && s[i] != '\0')
{
*j = *j + 1;
while (s[i] != c && s[i] != '\0')
i++;
}
}
}
static void ft_split_tab(char const *s, char **str, char c, int *j)
{
int len;
int i;
i = 0;
while (s[i] != '\0')
{
len = 0;
while (s[i] == c)
i++;
if (s[i] != c && s[i] != '\0')
{
while (s[i + len] != c && s[i + len] != '\0')
len++;
str[*j] = ft_strsub(s, i, len);
*j = *j + 1;
}
i = i + len;
}
str[*j] = NULL;
}
char **ft_strsplit(char const *s, char c)
{
char **str;
int j;
j = 1;
if (s == NULL)
return (NULL);
ft_split_count(s, c, &j);
str = (char **)malloc(sizeof(char *) * j);
if (str != NULL)
{
j = 0;
ft_split_tab(s, str, c, &j);
}
return (str);
}