-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
78 lines (70 loc) · 2.06 KB
/
ft_split.c
File metadata and controls
78 lines (70 loc) · 2.06 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ypetruzz <marvin@42lausanne.ch> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/26 16:40:05 by ypetruzz #+# #+# */
/* Updated: 2021/10/15 17:41:13 by ypetruzz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_sz_w(const char *str, char charset);
static int ft_wrd_count(const char *str, char charset);
static int ft_sz_w(const char *str, char charset)
{
int count;
count = 0;
while (str[count] && str[count] != charset)
{
count++;
}
return (count);
}
static int ft_wrd_count(const char *str, char charset)
{
int size;
int nb_words;
int count;
count = 0;
nb_words = 0;
while (*(str + count))
{
while (*(str + count) && *(str + count) == charset)
count++;
size = ft_sz_w(str + count, charset);
if (size)
{
nb_words++;
count += size;
}
}
return (nb_words);
}
char **ft_split(char const *s, char c)
{
t_init vars;
vars.i = 0;
vars.count = 0;
if (!s)
return (NULL);
vars.tab = malloc((ft_wrd_count(s, c) + 1) * sizeof(char *));
if (!vars.tab)
return (NULL);
while (*(s + vars.count))
{
while (*(s + vars.count) && *(s + vars.count) == c)
vars.count++;
if (ft_sz_w(s + vars.count, c))
{
vars.wrd = ft_substr(s, vars.count, ft_sz_w(s + vars.count, c));
if (!vars.wrd)
return (NULL);
vars.count += ft_sz_w(s + vars.count, c);
vars.tab[vars.i++] = vars.wrd;
}
}
vars.tab[vars.i] = (void *)0;
return (vars.tab);
}