-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
84 lines (76 loc) · 1.79 KB
/
Copy pathft_split.c
File metadata and controls
84 lines (76 loc) · 1.79 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
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: musyilma <musyilma@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/02 08:50:01 by musyilma #+# #+# */
/* Updated: 2024/11/02 15:13:22 by musyilma ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
static void check(char **str, int len)
{
while (len)
{
free(str[len]);
len--;
}
free(str);
}
static void spt(char **str, char const *s, char c)
{
int i;
int start;
int index;
i = 0;
start = 0;
index = 0;
while (s[i])
{
while (s[i] == c)
i++;
start = i;
while (s[start] != c && s[start])
start++;
if (s[start] == c || start > i)
{
str[index] = ft_substr(s, i, start - i);
if (str[index] == NULL)
check(str, index - 1);
i = start;
index++;
}
}
str[index] = NULL;
}
static int word(char const *s, char c)
{
int i;
int len;
i = 0;
len = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i])
len++;
while (s[i] && s[i] != c)
i++;
}
return (len);
}
char **ft_split(char const *s, char c)
{
char **a;
int len;
len = word(s, c);
a = malloc((len + 1) * sizeof(char *));
if (!a)
return (NULL);
spt(a, s, c);
return (a);
}