-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
82 lines (73 loc) · 1.39 KB
/
ft_split.c
File metadata and controls
82 lines (73 loc) · 1.39 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
#include "libft.h"
static int ft_word_counter(char const *s, char c)// c = delimitador desde y hata donde se genera el string, static porque solo sirve para una un script seria "privado" para que no se pueda cambiar ni usar desde afuera.
{
int i;
int count;
i = 0;
count = 0;
while (s[i])
{
while (s[i] && s[i] == c)
i++;
if (s[i])
count++;
while (s[i] && s[i] != c)
i++;
}
return (count);
}
static int ft_word_len(char const *s, char c)
{
int i;
i = 0;
while (s[i] && s[i] != c)
i++;
return (i);
}
static char **ft_free(char **tab, int i)
{
while (i >= 0)
{
free (tab[i]);
i--;
}
free(tab);
return (NULL);
}
static char **ft_fill_tab(char const *s, char c, char **tab)
{
int i;
int j;
int k;
i = 0;
j = 0;
while (s[i])
{
while (s[i] && s[i] == c)
i++;
if (s[i])
{
k = 0;
tab[j] = malloc(sizeof(char) * (ft_word_len(s + i, c) + 1));
if (!tab[j])
return (ft_free(tab, j));
while (s[i] && s[i] != c)
tab[j][k++] = s[i++];
tab[j++][k] = '\0';
}
}
tab[j] = NULL;
return (tab);
}
char **ft_split(char const *s, char c)// guarda los punteros por eso es doble puntero, guarda la posicion de memoria que te lleva al puntero
{
char **tab;// substring
if (!s)
return (NULL);
tab = malloc(sizeof(char *) * (ft_word_counter(s, c) + 1));
if (tab == NULL)
return (NULL);
tab = ft_fill_tab(s, c, tab);
return (tab);
}
devuelve punteros