Repository navigation
Expand file tree
/
Copy pathft_split.c
More file actions
84 lines (75 loc) · 2.01 KB
/
Copy pathft_split.c
File metadata and controls
84 lines (75 loc) · 2.01 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jode-cas <jode-cas@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/07/27 16:40:57 by jode-cas #+# #+# */
/* Updated: 2025/07/27 17:43:52 by jode-cas ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stddef.h>
#include <stdlib.h>
static void free_array(char **s)
{
int i;
i = 0;
while (s[i])
{
free(s[i]);
i++;
}
free(s);
}
static int fill_array(char **split, size_t word_count, char *start, char c)
{
size_t i;
char *end;
i = 0;
while (i < word_count)
{
while (*start == c)
start++;
end = start;
while (*end && *end != c)
end++;
split[i] = ft_calloc(end - start + 1, sizeof(char));
if (!split[i])
{
free_array(split);
return (0);
}
ft_memcpy(split[i], start, end - start);
start = end;
i++;
}
return (1);
}
static size_t count_words(char const *s, char delimiter)
{
size_t counter;
counter = 0;
while (*s)
{
if (*s != delimiter && (*(s + 1) == delimiter || *(s + 1) == '\0'))
counter++;
s++;
}
return (counter);
}
char **ft_split(char const *s, char delimiter)
{
size_t word_count;
char **split_array;
if (!s)
return (0);
word_count = count_words(s, delimiter);
split_array = ft_calloc(word_count + 1, sizeof(char *));
if (!split_array)
return (0);
if (!fill_array(split_array, word_count, (char *)s, delimiter))
return (0);
return (split_array);
}