-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
81 lines (73 loc) · 1.95 KB
/
ft_split.c
File metadata and controls
81 lines (73 loc) · 1.95 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: nsterk <nsterk@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2020/10/30 19:00:03 by nsterk #+# #+# */
/* Updated: 2021/03/03 01:40:39 by nsterk ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
static char **free_split(char **rstr, int len)
{
while (len)
{
len--;
free(rstr[len]);
}
free(rstr);
return (NULL);
}
static char **make_substrs(char **rstr, char const *s, char c, size_t len)
{
size_t i;
size_t j;
j = 0;
while (*s == (unsigned char)c)
s++;
while (j < len)
{
i = 0;
while (s[i] != '\0' && s[i] != (unsigned char)c)
i++;
rstr[j] = (char *)malloc(sizeof(char) * (i + 1));
if (!rstr[j])
return (free_split(rstr, (int)j));
ft_strlcpy(rstr[j], s, (i + 1));
while (s[i] == (unsigned char)c)
i++;
s += i;
j++;
}
rstr[j] = NULL;
return (rstr);
}
static size_t find_amount(char const *s, char c)
{
size_t i;
size_t len;
i = 0;
len = 0;
while (s[i] != '\0')
{
if ((s[i] != c) && ((s[i + 1] == c) || (s[i + 1] == '\0')))
len++;
i++;
}
return (len);
}
char **ft_split(char const *s, char c)
{
char **rstr;
size_t len;
if (!s)
return (NULL);
len = find_amount(s, c);
rstr = (char **)malloc(sizeof(char *) * (len + 1));
if (!rstr)
return (NULL);
rstr = make_substrs(rstr, s, c, len);
return (rstr);
}