-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_split.c
105 lines (94 loc) · 2.81 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jede-ara <jede-ara@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/08 13:19:04 by jede-ara #+# #+# */
/* Updated: 2022/11/15 15:36:55 by jede-ara ### ########.fr */
/* */
/* ************************************************************************** */
/*
DESCRIÇÃO: split() aloca (com malloc(3)) e retorna um array de strings
obtido pela divisão de 's' usando o caractere 'c' como delimitador, ou seja,
divide a string com caractere especificado como delimitador, em um array de
strings. A matriz deve ser finalizada por um ponteiro NULL.
*/
#include "libft.h"
/*A função ft_wordcount tem a finalidade de contar a quantidade de palavras no
array de strings. Parâmetros: 's' string contendo cada palavra e 'c' caractere
usado como delimitador. Return retornará quantas palavras foram encontradas.*/
static int ft_wordcount(const char *s, char c)
{
int i;
int j;
i = 0;
j = 0;
while (s[i])
{
while (s[i] == c)
i++;
if (s[i] != '\0' )
j++;
while (s[i] && (s[i] != c))
i++;
}
return (j);
}
/*A função ft_wordcpy tem como finalidade localizar a próxima palavra em
uma matriz. Parâmetros: 's' string contendo cada palavra, 'c' caractere usado
como delimitador e 'strs' matriz de caracteres armazenando cada palavra
individualmente. Return retornará um ponteiro de caractere para a próxima
palavra encontrada.*/
static char *ft_wordcpy(const char *s, char c, char **strs)
{
char *next;
next = ft_strchr(s, c);
if (!next || !c)
{
next = (char *)s;
while (*next)
next++;
}
*strs = ft_substr(s, 0, next - s);
return (next);
}
char **ft_split(char const *s, char c)
{
char **strs;
char **buffer;
if (!s)
return (NULL);
strs = (char **)malloc((ft_wordcount(s, c) + 1) * sizeof(*strs));
if (!strs)
return (NULL);
buffer = strs;
while (*s)
{
while (*s == c)
s++;
if (*s)
s = ft_wordcpy(s, c, strs++);
}
*strs = 0;
return (buffer);
}
/*#include <stdio.h>
int main()
{
char *string = "dividir";
char delimitador = 'v';
char **dividido;
int size;
int i;
i = 0;
size = ft_wordcount(string, delimitador) + 1;
dividido = ft_split(string, delimitador);
while(i < size)
{
printf("->%s\n", dividido[i++]);
}
printf("->%s", dividido[i]);
return(0);
}*/