-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
117 lines (109 loc) · 2.38 KB
/
ft_split.c
File metadata and controls
117 lines (109 loc) · 2.38 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rafael-m <rafael-m@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/04/11 12:34:44 by rafael-m #+# #+# */
/* Updated: 2025/06/04 11:56:23 by rafael-m ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//#include <string.h>
//#include <stdio.h>
static char *ft_sep_lines(char *str, int depth, char charset)
{
int count;
int i;
int start;
count = 0;
i = 0;
while (str[i])
{
while (str[i] == charset)
i++;
if (!str[i])
break ;
if (count == depth)
{
start = i;
while (str[i] && str[i] != charset)
i++;
return (ft_substr(str, start, i - start));
}
while (str[i] && str[i] != charset)
i++;
count++;
}
return (NULL);
}
static int ft_depth(char *str, char charset)
{
int depth;
int word;
depth = 0;
word = 0;
while (*str)
{
if (*str != charset && !word)
{
depth++;
word = 1;
}
if (*str == charset)
word = 0;
str++;
}
return (depth);
}
char **ft_split(char *str, char charset)
{
char **result;
int depth;
int i;
i = 0;
if (!str || !charset)
return (NULL);
depth = ft_depth(str, charset);
result = (char **)malloc((depth + 1) * sizeof(char *));
if (!result)
return (NULL);
while (i < depth)
{
result[i] = ft_sep_lines(str, i, charset);
if (!result[i])
{
while (i-- > 0)
free(result[i]);
free(result);
return (NULL);
}
i++;
}
result[depth] = NULL;
return (result);
}
/*
int main(void)
{
int i = 0;
char **result = ft_split(" lorem ipsum Suspendisse ", ' ');
if (!result)
return (1);
while (result[i])
{
printf("result[%d] = %s\n", i, result[i]);
i++;
}
i = 0;
while (result[i])
{
free(result[i]);
i++;
}
// printf("depth = %d\n", depth);
free (result[i]);
free(result);
return (0);
}*/