-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
81 lines (74 loc) · 1.89 KB
/
Copy pathft_split.c
File metadata and controls
81 lines (74 loc) · 1.89 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
81
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rrebois <rrebois@student.42lyon.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/14 16:27:55 by rrebois #+# #+# */
/* Updated: 2022/11/22 12:13:52 by rrebois ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **ft_free(char **ptr, size_t j)
{
size_t x;
x = 0;
while (x < j)
{
free(ptr[x]);
x++;
}
free(ptr);
return (NULL);
}
static char **ft_cut(char const *s, char c, char **ptr, size_t count)
{
size_t i;
size_t j;
size_t start;
i = 0;
j = 0;
while (s[i] != '\0')
{
while (s[i] == c && s[i] != '\0')
i++;
if (s[i] != c && s[i] != '\0')
start = i;
while (s[i] != c && s[i] != '\0')
i++;
if (j < count)
{
ptr[j] = ft_substr(s, start, i - start);
if (ptr[j] == NULL)
return (ft_free(ptr, j));
j++;
}
}
ptr[j] = 0;
return (ptr);
}
char **ft_split(char const *s, char c)
{
size_t i;
size_t count;
char **ptr;
i = 0;
count = 0;
if (s == NULL)
return (NULL);
while (s[i] != '\0')
{
while (s[i] == c && s[i] != '\0')
i++;
if (s[i] != c && s[i] != '\0')
count++;
while (s[i] != c && s[i] != '\0')
i++;
}
ptr = (char **)malloc(sizeof(*ptr) * (count + 1));
if (ptr == NULL)
return (NULL);
ptr = ft_cut(s, c, ptr, count);
return (ptr);
}