-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
81 lines (74 loc) · 1.85 KB
/
Copy pathft_strsplit.c
File metadata and controls
81 lines (74 loc) · 1.85 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: enaboule <enaboul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/01/28 15:18:15 by enaboule #+# #+# */
/* Updated: 2016/01/28 15:45:23 by enaboule ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
static int ft_nbwords(char const *s, char c)
{
int cpt;
int nb_words;
cpt = 0;
nb_words = 0;
while (s[cpt] == c)
cpt++;
while (s[cpt])
{
if (s[cpt] == c)
{
while (s[cpt] == c)
cpt++;
nb_words++;
}
else
{
while (s[cpt] != c && s[cpt])
cpt++;
}
}
if (s[cpt - 1] != c)
nb_words++;
return (nb_words);
}
static char **ft_nb_size(char **array, int nbr, char const *s, char c)
{
int cpt;
int i;
int j;
cpt = 0;
i = 0;
while (cpt < nbr)
{
while (s[i] == c)
i++;
if (s[i] == '\0')
break ;
j = i;
while (s[j] != c)
j++;
array[cpt] = ft_strsub(s, i, (j - i));
cpt++;
i = j;
}
array[cpt] = NULL;
return (array);
}
char **ft_strsplit(char const *s, char c)
{
char **array;
int nbr;
if (s == NULL)
return (NULL);
nbr = ft_nbwords(s, c);
if ((array = (char **)malloc(sizeof(char *) * (nbr + 1))) == NULL)
return (NULL);
array = ft_nb_size(array, nbr, s, c);
return (array);
}