-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
74 lines (67 loc) · 1.77 KB
/
ft_split.c
File metadata and controls
74 lines (67 loc) · 1.77 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbiasuz <lbiasuz@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/19 21:35:10 by lbiasuz #+# #+# */
/* Updated: 2022/04/23 09:43:24 by lbiasuz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int split_count(char const *s, char c)
{
int i;
char *a;
i = 0;
a = (char *) s;
while (*a)
{
while (*a == c)
a++;
if (!(*a))
break ;
while (*a != c && *a != 0)
a++;
i++;
}
return (i);
}
static int word_len(char const *s, char c)
{
unsigned int i;
char *a;
i = 0;
a = (char *) s;
while (*a == c)
a++;
while (a[i] && a[i] != c)
i++;
return (i);
}
char **ft_split(char const *s, char c)
{
char **tab;
char *aux;
int wc;
int j;
j = 0;
wc = split_count((char *) s, c);
tab = malloc(sizeof(char **) * (wc + 1));
if (!tab)
return (NULL);
while (split_count(&s[j], c))
{
while (s[j] == c)
j++;
aux = malloc(sizeof(char) * (word_len(&s[j], c) + 1));
if (!aux)
return (NULL);
ft_strlcpy(aux, &s[j], word_len(&s[j], c) + 1);
tab[wc - split_count(&s[j], c)] = aux;
j += word_len(&s[j], c);
}
tab[wc] = NULL;
return (tab);
}