-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
76 lines (69 loc) · 1.74 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
76
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpires-n <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/07 21:23:10 by lpires-n #+# #+# */
/* Updated: 2022/06/10 19:38:38 by lpires-n ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_string(char const *s, char c)
{
int i;
int n_word;
int control;
i = 0;
control = 0;
n_word = 0;
while (s[i] != '\0')
{
if (s[i] != c && control == 0)
{
control = 1;
n_word++;
}
else if (s[i] == c)
control = 0;
i++;
}
return (n_word);
}
static void add_string(char const *s, char c, char **split)
{
size_t len;
size_t i;
size_t j;
j = 0;
i = 0;
len = 0;
while (i <= ft_strlen(s))
{
if (s[i] == c || s[i] == '\0')
{
if (i != len)
{
split[j] = ft_substr(s, len, i - len);
if (split[j] == NULL)
free(split);
j++;
}
len = i + 1;
}
i++;
}
}
char **ft_split(char const *s, char c)
{
char **split;
int i;
i = count_string(s, c);
split = malloc(sizeof(char *) * (i + 1));
if (split == NULL)
return (NULL);
add_string(s, c, split);
split[i] = NULL;
return (split);
}