-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_split.c
63 lines (58 loc) · 1.59 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rde-noro <rde-noro@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 23:28:13 by rde-noro #+# #+# */
/* Updated: 2022/10/11 03:09:16 by rde-noro ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int count_str(char const *s, char c)
{
int count;
int i;
count = 0;
i = 0;
if (!s)
return (0);
while (s[i])
{
if (i == 0 && s[i] == c)
i++;
if (s[i] != c && s[i + 1] == c)
count++;
if (s[i] != c && s[i + 1] == '\0')
count++;
i++;
}
return (count);
}
char **ft_split(char const *s, char c)
{
int isstr;
int i;
int itab;
char **tab;
isstr = 0;
i = 0;
itab = 0;
tab = (char **) malloc (sizeof(char *) * (count_str(s, c) + 1));
while (s[i] && count_str(s, c) > 0)
{
while (s[i] && s[i] == c)
i++;
isstr = i;
while (s[i] && s[i] != c)
i++;
if (i > isstr)
{
tab[itab] = ft_substr(s, isstr, i - isstr);
itab++;
}
}
tab[itab] = NULL;
return (tab);
}