-
Notifications
You must be signed in to change notification settings - Fork 0
/
split.c
107 lines (96 loc) · 2.34 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alvutina <alvutina@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/12 10:15:23 by alvutina #+# #+# */
/* Updated: 2024/06/12 10:27:05 by alvutina ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int ft_is_a_sep(char c, char *sep)
{
int i;
i = 0;
while (sep[i])
{
if (sep[i] == c)
return (1);
i++;
}
return (0);
}
static int ft_usep(char *str, char *sep)
{
int i;
i = 1;
while (ft_is_a_sep(str[i], sep) == 0 && str[i])
i++;
return (i);
}
static int ft_count_malloc(char *str, char *sep)
{
int num_sep;
int i;
int verification;
num_sep = 0;
verification = 1;
i = 0;
while (str[i])
{
if (verification == 0 && ft_is_a_sep(str[i], sep))
verification = 1;
if (verification == 1 && ft_is_a_sep(str[i], sep) == 0)
verification = 2;
if (verification == 2 && (ft_is_a_sep(str[i], sep)
|| str[i + 1] == '\0'))
{
verification = 0;
num_sep++;
}
else
i++;
}
return (num_sep);
}
static void *ft_for_free(char **strs, int k)
{
int i;
i = 0;
while (i < k)
{
free(strs[i]);
i++;
}
free(strs[i]);
return (NULL);
}
char **ft_split_mult(char *str, char *charset)
{
char **strs;
int num_sep;
int i;
int j;
int k;
num_sep = ft_count_malloc(str, charset);
strs = malloc(sizeof(char *) * (num_sep + 1));
i = 0;
k = -1;
while (++k < num_sep)
{
j = -1;
while (ft_is_a_sep(str[i], charset) == 1 && str[i])
i++;
strs[k] = malloc(sizeof(char) * (ft_usep(str + i, charset) + 1));
if (strs[k] == NULL)
return (ft_for_free(strs, k));
while (str[i] && ft_is_a_sep(str[i], charset) == 0 && ++j > -10)
strs[k][j] = str[i++];
strs[k][j + 1] = '\0';
i++;
}
strs[k] = 0;
return (strs);
}