-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
executable file
·107 lines (97 loc) · 2.29 KB
/
Copy pathft_strsplit.c
File metadata and controls
executable file
·107 lines (97 loc) · 2.29 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mnaji <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/16 22:18:13 by mnaji #+# #+# */
/* Updated: 2018/12/05 00:06:21 by mnaji ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_count_word(char const *s, char c)
{
int i;
int count_word;
i = 0;
count_word = 0;
while (s[i] != '\0')
{
while (s[i] == c)
{
i++;
if (s[i] == '\0')
return (count_word);
}
while (s[i] != c)
{
if (s[i] == '\0')
return (count_word + 1);
i++;
}
count_word++;
}
return (count_word);
}
static int ft_size_word(char *str, char c)
{
int i;
int count;
i = 0;
count = 0;
while (str[i] != c && str[i] != '\0')
{
count++;
i++;
}
return (count);
}
static int ft_cpy_ligne(char *tab, char *s, int i, char c)
{
int t;
t = 0;
while (s[i] != c && s[i] != 0)
{
tab[t] = s[i];
i++;
t++;
}
tab[t] = '\0';
return (i);
}
static char **ft_return_tab(char *s, char **tab, int ligne, char c)
{
if (*s - 1 != c)
tab[ligne] = 0;
else
tab[ligne - 1] = 0;
return (tab);
}
char **ft_strsplit(char const *s, char c)
{
char **tab;
int i;
int ligne;
if (!s || !c || !(tab = (char**)malloc(sizeof(char*) *\
(ft_count_word(s, c) + 1))))
return (NULL);
ligne = 0;
i = 0;
while (ligne < ft_count_word(s, c))
{
while (s[i] == c && s[i] != 0)
i++;
if (!(tab[ligne] = (char*)malloc(sizeof(char) * \
ft_size_word((char*)&s[i], c) + 1)))
{
while (ligne-- >= 0)
free(tab[ligne]);
free(tab);
return (NULL);
}
i = ft_cpy_ligne(tab[ligne], (char*)s, i, c);
ligne++;
}
return (ft_return_tab((char*)&s[i], tab, ligne, c));
}