-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
75 lines (67 loc) · 1.9 KB
/
ft_split.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
73
74
75
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: inwagner <inwagner@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/03 18:25:16 by inwagner #+# #+# */
/* Updated: 2023/06/09 15:15:34 by inwagner ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_substr_counter(char const *str, char c)
{
int i;
int count;
count = 0;
i = 0;
while (str[i])
{
while (str[i] == c && str[i])
i++;
if (str[i])
count++;
while (str[i] != c && str[i])
i++;
}
return (count);
}
static int ft_substr_len(char const *s, int start, char c)
{
int len;
len = 0;
while (s[start] != c && s[start++])
len++;
return (len);
}
char **ft_split(char const *str, char c)
{
int i;
int start;
int l_sstr;
int n_sstr;
char **pbox;
if (!str)
return (NULL);
n_sstr = ft_substr_counter(str, c);
pbox = (char **)ft_calloc((n_sstr + 1), sizeof(char *));
if (!pbox)
return (NULL);
start = 0;
i = 0;
while (i < n_sstr)
{
while (str[start] && str[start] == c)
start++;
l_sstr = ft_substr_len(str, start, c);
pbox[i] = ft_substr(str, start, l_sstr);
start += l_sstr;
i++;
}
return (pbox);
}
/*
Picota `s` em várias strings utilizando `c` como um delimitador, deve ter nulo em cada string.
Retorna as strings ou nulo se a alocação falhar.
*/