-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
97 lines (86 loc) · 1.97 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cnatanae <cnatanae@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/17 09:17:44 by cnatanae #+# #+# */
/* Updated: 2023/11/11 12:20:52 by cnatanae ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_words(char const *s, char c)
{
int words;
words = 0;
if (!s)
return (0);
while (*s)
{
while (*s == c && *s)
s++;
if ((*s != c) && *s)
words++;
while (*s != c && *s)
s++;
}
return (words);
}
static int ft_size(char *s, char c)
{
int i;
i = 0;
while (s[i] != c && s[i])
i++;
return (i);
}
static char *ft_put_letter(char *s, char c)
{
int i;
char *str;
i = 0;
str = (char *)ft_calloc(1, sizeof(char) * ft_size(s, c) + 1);
if (!str)
return (NULL);
while (s[i] != c && s[i])
{
str[i] = s[i];
i++;
}
return (str);
}
void ft_free(char **split)
{
int idx;
idx = 0;
while (split[idx])
free(split[idx++]);
free(split);
}
char **ft_split(char const *s, char c)
{
int idx_b;
char **split;
idx_b = 0;
split = (char **)ft_calloc((ft_count_words(s, c) + 1), sizeof(char *));
if (!split || !s)
return (0);
while (*s)
{
if (*s != c)
{
split[idx_b] = ft_put_letter((char *)s, c);
if (!split[idx_b++])
{
ft_free(split);
return (NULL);
}
while (*s != c && *s)
s++;
}
else
s++;
}
return (split);
}