-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
88 lines (80 loc) · 1.94 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
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dbrandao <dbrandao@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/17 15:14:42 by dbrandao #+# #+# */
/* Updated: 2022/07/04 05:03:09 by dbrandao ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//jumps addresses
static char *jump(char *s, char c)
{
while (*s != c && *s)
s++;
while (*s == c && *s)
s++;
return (s);
}
//returns the number of strings to be created
static int split_len(char *s, char c)
{
int len;
len = 0;
while (*s)
{
if (*s != c)
len++;
while (*s != c && *s)
s++;
while (*s == c && *s)
s++;
}
return (len);
}
//create new string
static char *new_str(char *str, char c)
{
int i;
char *new;
i = 0;
while (str[i] != c && str[i])
i++;
if (!i)
return (NULL);
new = (char *) malloc(i + 1);
if (!new)
return (NULL);
ft_strlcpy(new, str, i + 1);
return (new);
}
char **ft_split(char const *s, char c)
{
int len;
int i;
char *s1;
char **strings;
if (!s)
return (NULL);
while (*s == c && *s)
s++;
s1 = (char *) s;
len = split_len(s1, c);
strings = (char **) malloc(sizeof(char *) * (len + 1));
if (!strings)
return (NULL);
i = 0;
while (i < len)
{
strings[i] = new_str(s1, c);
if (!strings[i])
return (NULL);
s1 = jump(s1, c);
i++;
}
strings[i] = NULL;
return (strings);
}