-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
78 lines (70 loc) · 1.88 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: frgutier <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/19 19:37:43 by frgutier #+# #+# */
/* Updated: 2022/09/24 17:22:16 by frgutier ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_count_words(char const *s, char c)
{
size_t i;
size_t count;
i = 0;
count = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] != c && s[i] != '\0')
count++;
while (s[i] != c && s[i] != '\0')
i++;
}
return (count);
}
static size_t ft_word_len(char const *s, char c)
{
size_t i;
i = 0;
while (s[i] != c && s[i] != '\0')
i++;
return (i);
}
static char **ft_free(char **str, size_t i)
{
while (i--)
free(str[i]);
free(str);
return (NULL);
}
char **ft_split(char const *s, char c)
{
char **str;
size_t i;
size_t j;
size_t k;
i = 0;
j = 0;
str = (char **)malloc(sizeof(char *) * (ft_count_words(s, c) + 1));
if (!s || !str)
return (NULL);
while (i < ft_count_words(s, c))
{
while (s[j] == c)
j++;
str[i] = (char *)malloc(sizeof(char) * (ft_word_len(&s[j], c) + 1));
if (!str[i])
return (ft_free(str, i));
k = 0;
while (s[j] != c && s[j] != '\0')
str[i][k++] = s[j++];
str[i++][k] = '\0';
}
str[i] = NULL;
return (str);
}