-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
151 lines (137 loc) · 2.87 KB
/
ft_split.c
File metadata and controls
151 lines (137 loc) · 2.87 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: simarcha <simarcha@student.42barcelona. +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/26 14:06:14 by simarcha #+# #+# */
/* Updated: 2024/09/30 11:34:32 by simarcha ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
//We need a function for counting the words
//We need a function to draw our matrix with the good numbers of cells
//we need a function to copy the exact same letters in our matrix
//we need a function to free everything if it fails
//we need a function to gather everything
static int countwords(const char *s, char c)
{
int wc;
int i;
int check;
wc = 0;
i = 0;
check = 0;
while (s[i])
{
if (s[i] == c && check != 0)
{
wc++;
check = 0;
}
else if (s[i] != c)
check++;
i++;
}
if (check > 0)
wc++;
return (wc);
}
static char **draw_array(char **array, const char *s, char c)
{
int i;
int x;
int count;
i = 0;
count = 0;
x = 0;
while (s[i])
{
if (s[i] != c)
count++;
if ((s[i] == c && count > 0) || (s[i] != c && s[i + 1] == '\0'))
{
array[x] = malloc((count + 1) * sizeof(char));
if (!array[x])
return (NULL);
x++;
count = 0;
}
i++;
}
return (array);
}
static char **complete_array(char **array, const char *s, char c)
{
int i;
int x;
int y;
i = 0;
x = 0;
y = 0;
while (s[i])
{
if (s[i] != c)
array[x][y++] = s[i];
if ((s[i] == c && i > 0 && s[i - 1] != c)
|| (s[i] != c && s[i + 1] == 0))
{
array[x][y] = '\0';
x++;
y = 0;
}
i++;
}
return (array);
}
char **free_array(char **array)
{
int x;
x = 0;
while (array[x])
{
free(array[x]);
array[x] = NULL;
x++;
}
free(array);
array = NULL;
return (array);
}
char **ft_split(const char *s, char c)
{
char **array;
int wc;
wc = countwords(s, c);
array = malloc((wc + 1) * sizeof(*array));
if (!array)
return (NULL);
if (draw_array(array, s, c))
{
complete_array(array, s, c);
array[wc] = NULL;
}
else
array = free_array(array);
return (array);
}
/*
#include <stdio.h>
#include <string.h>
int main(void)
{
char str[90] = "";
char **array;
int x;
// printf("%zu\n", strlen(str));
// printf("%i\n", countwords(str, ' '));
x = 0;
array = ft_split(str, ' ');
while (array[x])
{
printf("%s\n", array[x]);
x++;
}
return (0);
}*/