-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
67 lines (61 loc) · 1.77 KB
/
ft_split.c
File metadata and controls
67 lines (61 loc) · 1.77 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ferncarv <ferncarv@student.42.rio> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/24 22:35:33 by fernanda #+# #+# */
/* Updated: 2022/06/02 11:50:02 by ferncarv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **ft_first(char **count, int sep, char const *s, char c);
char **ft_split(char const *s, char c)
{
char **count;
int index;
int sep;
sep = 0;
index = 0;
if (!s)
return (NULL);
while (s[index])
{
if (s[index] != c && (s[index + 1] == c || s[index + 1] == '\0'))
sep++;
index++;
}
count = (char **)malloc(sizeof(char *) * (sep + 1));
if (!count)
return (NULL);
ft_first(count, sep, s, c);
return (count);
}
static char **ft_first(char **count, int sep, char const *s, char c)
{
int aux1;
int aux2;
int index;
aux1 = 0;
aux2 = 0;
index = -1;
while (++index < sep)
{
aux1 = aux1 + aux2;
while (s[aux1] == c)
aux1++;
aux2 = 0;
while (s[aux1 + aux2] != c)
aux2++;
count[index] = ft_substr(s, aux1, aux2);
if (!count[index])
{
while (index)
free(count[index--]);
return (NULL);
}
}
count[index] = NULL;
return (count);
}