-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
70 lines (63 loc) · 1.73 KB
/
ft_split.c
File metadata and controls
70 lines (63 loc) · 1.73 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: leramos- <leramos-@student.42lisboa.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/14 15:43:56 by leramos- #+# #+# */
/* Updated: 2025/04/23 13:36:53 by leramos- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t count_words(char const *str, char c)
{
size_t count;
if (!*str)
return (0);
count = 0;
while (*str)
{
while (*str == c)
str++;
if (*str)
count++;
while (*str && *str != c)
str++;
}
return (count);
}
static char **fill_array(char **array, char const *str, char c)
{
size_t i;
size_t str_len;
i = 0;
while (*str)
{
while (*str == c && *str)
str++;
if (*str)
{
if (!ft_strchr(str, c))
str_len = ft_strlen(str);
else
str_len = ft_strchr(str, c) - str;
array[i] = ft_substr(str, 0, str_len);
str += str_len;
i++;
}
}
array[i] = NULL;
return (array);
}
char **ft_split(char const *s, char c)
{
char **array;
if (!s)
return (NULL);
array = malloc((count_words(s, c) + 1) * sizeof(char *));
if (!array)
return (NULL);
array = fill_array(array, s, c);
return (array);
}