-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_split.c
76 lines (68 loc) · 1.79 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hdescamp <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/07 12:00:52 by hdescamp #+# #+# */
/* Updated: 2024/11/14 16:13:29 by hdescamp ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static int is_charset(char c, char charset)
{
if (charset == c)
return (1);
return (0);
}
static int ft_len(const char *str, char c)
{
int i;
int l;
l = 0;
i = 0;
while (str[i])
{
if (str[i] != c && (str[i + 1] == c || str[i + 1] == '\0'))
l++;
i++;
}
return (l);
}
static int ft_word_len(const char *str, char c)
{
int l;
int i;
l = 0;
i = -1;
while (str[++i] && !is_charset(str[i], c))
++l;
return (l);
}
char **ft_split(char const *s, char c)
{
char **tab;
int l;
int i;
int j;
tab = (char **)malloc(sizeof(char *) * (ft_len(s, c) + 1));
if (!tab)
return (NULL);
i = 0;
j = 0;
while (s[i] && j < ft_len(s, c))
{
while (s[i] && is_charset(s[i], c))
++i;
l = ft_word_len(s + i, c);
tab[j] = (char *)malloc(sizeof(char) * (l + 1));
if (!tab[j])
return (NULL);
ft_strlcpy(tab[j++], s + i, l + 1);
i += l;
}
tab[j] = NULL;
return (tab);
}