-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
70 lines (63 loc) · 1.67 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fatkeski <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/30 20:39:51 by fatkeski #+# #+# */
/* Updated: 2023/10/30 21:39:49 by fatkeski ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int wordcounter(char const *s, char c)
{
int i;
int count;
i = 0;
count = 0;
while (*s)
{
while (s[i] == c && s[i])
i++;
if (s[i] == '\0')
return (count);
while (s[i] != c && s[i])
i++;
count++;
}
return (count);
}
static int charlencounter(char const *s, char c)
{
int i;
i = 0;
while (s[i] && s[i] != c)
i++;
return (i);
}
char **ft_split(char const *s, char c)
{
int index;
char **str;
int i;
i = 0;
index = 0;
if (!s)
return (0);
str = (char **)malloc(sizeof(char *) * (wordcounter(s, c) + 1));
if (!str)
return (0);
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] == '\0')
break ;
str[index] = ft_substr(s + i, 0, charlencounter(s + i, c));
index++;
i += charlencounter(s + i, c);
}
str[index] = 0;
return (str);
}