-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
80 lines (72 loc) · 1.74 KB
/
ft_split.c
File metadata and controls
80 lines (72 loc) · 1.74 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
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asekkak <asekkak@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/06 13:23:02 by asekkak #+# #+# */
/* Updated: 2022/10/18 16:29:55 by asekkak ### ########.fr */
/* */
/* ************************************************************************** */
#include "./libft.h"
int len_word(char const *s, char c)
{
int count;
int i;
i = 0;
count = 0;
while (s[i])
{
while (s[i] && s[i] == c)
i++;
if (s[i])
count++;
while (s[i] && s[i] != c)
i++;
}
return (count);
}
int len_len_word(char const *s, char c, int i)
{
int len;
len = 0;
while (s[i] && s[i] != c)
{
i++;
len++;
}
return (len);
}
void *ft_free(char **ptr, int j)
{
while (j-- > 0)
free(ptr[j]);
free(ptr);
return (0);
}
char **ft_split(char const *s, char c)
{
char **ptr;
int j;
int i;
j = 0;
i = 0;
if (!s)
return (0);
ptr = (char **)malloc(sizeof(char *) * (len_word(s, c) + 1));
if (!ptr)
return (0);
while (j < len_word(s, c))
{
while (s[i] && s[i] == c)
i++;
ptr[j] = ft_substr(s, i, len_len_word(s, c, i));
if (!ptr)
ft_free(ptr, j);
j++;
i += len_len_word(s, c, i);
}
ptr[j] = 0;
return (ptr);
}