This repository has been archived by the owner on Feb 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
96 lines (87 loc) · 2.03 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
89
90
91
92
93
94
95
96
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: faubert <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/02 21:32:33 by faubert #+# #+# */
/* Updated: 2020/02/04 12:40:06 by faubert ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int str_nb(char const *s, char c)
{
int str_nb;
str_nb = 0;
while (*s)
{
if (*s != c)
{
str_nb++;
while (*s && *s != c)
s++;
}
while (*s && *s == c)
s++;
}
return (str_nb);
}
static char *ft_strcp(char *dest, char const *src, char c)
{
while (*src && *src != c)
{
*dest = *src;
dest++;
src++;
}
*dest = '\0';
return (dest);
}
static char *cp_word(char const *s, char c)
{
int i;
char *str_nb;
i = 0;
while (s[i] && s[i] != c)
i++;
if (!(str_nb = ft_calloc(sizeof(char), (i + 1))))
return (NULL);
i = 0;
ft_strcp(str_nb, s, c);
return (str_nb);
}
static char **free_arr(char **arr, int i)
{
while (i--)
free(arr[i]);
free(arr);
return (0);
}
char **ft_split(char const *s, char c)
{
char **strs;
int i;
int j;
if (!s)
return (NULL);
if (!(strs = (char**)malloc(sizeof(char*) * (str_nb(s, c) + 1))))
return (NULL);
i = 0;
j = 0;
while (s[j])
{
while (s[j] && s[j] == c)
j++;
if (s[j] && s[j] != c)
{
if (!(strs[i] = cp_word(&s[j], c)))
return (free_arr(strs, i));
i++;
while (s[j] && s[j] != c)
j++;
}
}
strs[i] = NULL;
return (strs);
}