-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
107 lines (97 loc) · 2.79 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
98
99
100
101
102
103
104
105
106
107
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: meghribe <meghribe@student.42barcelona.co +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/11 22:06:57 by meghribe #+# #+# */
/* Updated: 2024/11/14 13:20:07 by meghribe ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
/* TODO: COMPARE WITH THE ORIGINAL ONE */
/* Count the number of strings in the array */
/* To count the number of strings, we iterate through the string s */
/* If the character is not the separator, we increment the total */
/* We then skip the string until we find the separator */
/* We repeat the process until we reach the end of the string */
static size_t ft_array_len(char const *s, char c)
{
size_t i;
size_t total;
i = 0;
total = 0;
while (s[i] != '\0')
{
if (s[i] != c)
{
total++;
while (s[i] != '\0' && s[i] != c)
i++;
}
else
i++;
}
return (total);
}
/* Allocate memory for the string */
/* Copy the substring from the string s to the new string */
/* Return the new string */
static char *ft_next_string(char const *s, char c, size_t *start)
{
size_t i;
size_t length;
char *str;
i = *start;
while (s[i] != '\0' && s[i] == c)
i++;
*start = i;
length = 0;
while (s[i] != '\0' && s[i] != c)
{
length++;
i++;
}
str = ft_substr(s, *start, length);
*start = i;
return (str);
}
/* If the allocation fails, free the memory allocated for the strings */
static void ft_free_memory(char **str)
{
size_t i;
i = 0;
while (str[i])
free(str[i++]);
free(str);
}
/* Split the string s using the character c as separator */
/* If the string s is NULL, return NULL */
/* Count the number of strings in the array */
/* Allocate memory for the array of strings, if the alloc fails, return NULL */
/* Iterate through the string s */
char **ft_split(char const *s, char c)
{
size_t strcount;
size_t i;
size_t start;
char **result;
if (!s)
return (NULL);
strcount = ft_array_len(s, c);
result = (char **)malloc((strcount + 1) * sizeof(char *));
if (!result)
return (NULL);
i = 0;
start = 0;
while (i < strcount)
{
result[i] = ft_next_string(s, c, &start);
if (!result[i])
return (ft_free_memory(result), NULL);
i++;
}
result[i] = NULL;
return (result);
}