-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_strsplit.c
109 lines (98 loc) · 2.35 KB
/
ft_strsplit.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
106
107
108
109
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaraval <tmaraval@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/14 14:27:01 by tmaraval #+# #+# */
/* Updated: 2017/11/20 08:12:46 by tmaraval ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_index_mot(char *str, int mot, char separateur)
{
int i;
int cntword;
i = 0;
cntword = 0;
while (str[i])
{
while (str[i] == separateur)
i++;
if (cntword == mot)
return (i);
while (str[i] != separateur && str[i])
i++;
cntword++;
}
return (0);
}
static int ft_nb_mots(char *str, char separateur)
{
int i;
int nbword;
i = 0;
nbword = 0;
while (str[i])
{
if (str[i] == separateur)
i++;
else
{
while (str[i] != separateur && str[i])
i++;
nbword++;
}
}
return (nbword);
}
static int ft_nb_ltr(char *str, int mot, char separateur)
{
int index;
int nbletter;
index = ft_index_mot(str, mot, separateur);
nbletter = 0;
while (str[index] != separateur && str[index])
{
nbletter++;
index++;
}
return (nbletter);
}
static void ft_remplissage(char **tab_rt, char *str, int i, char c)
{
int j;
int k;
j = 0;
k = ft_index_mot(str, i, c);
while (str[k] != c && str[k])
{
tab_rt[i][j] = str[k];
k++;
j++;
}
tab_rt[i][j] = '\0';
}
char **ft_strsplit(char const *s, char c)
{
int i;
char **tab_rt;
char *str;
if (s == NULL)
return (NULL);
str = (char *)s;
i = 0;
if (!(tab_rt = (char**)malloc(sizeof(char*) * (ft_nb_mots(str, c) + 1))))
return (NULL);
while (i < ft_nb_mots(str, c))
{
tab_rt[i] = (char*)malloc(sizeof(char) * (ft_nb_ltr(str, i, c) + 1));
if (tab_rt[i] == NULL)
return (NULL);
ft_remplissage(tab_rt, str, i, c);
i++;
}
tab_rt[i] = NULL;
return (tab_rt);
}