-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_strsplit.c
More file actions
101 lines (92 loc) · 2.07 KB
/
ft_strsplit.c
File metadata and controls
101 lines (92 loc) · 2.07 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsplit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kbahrar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/03 19:28:48 by kbahrar #+# #+# */
/* Updated: 2019/04/25 21:49:57 by kbahrar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int how_many(char const *s, char c)
{
int i;
int temp;
temp = 0;
i = 0;
while (s[i] == c)
i++;
if (s[i])
temp = 1;
while (s[i])
{
if (s[i] == c)
{
while (s[i] == c)
i++;
if (s[i] == '\0')
return (temp);
temp++;
}
i++;
}
return (temp);
}
static int size_mallocc(char const *s, char c, int i)
{
int temp;
temp = 0;
while (s[i] == c)
i++;
while (s[i] != c && s[i])
{
temp++;
i++;
}
return (temp);
}
char **ta3mir(char **str, char const *s, char c, int i)
{
int j;
int temp;
j = 0;
temp = -1;
while (s[i])
{
str[j][++temp] = s[i];
i++;
if (s[i] == c || s[i] == '\0')
{
while (s[i] == c)
i++;
str[j][++temp] = '\0';
temp = -1;
j++;
if (s[i] != '\0')
str[j] = (char*)ft_memalloc(size_mallocc(s, c, i) + 1);
}
}
str[j] = NULL;
return (str);
}
char **ft_strsplit(char const *s, char c)
{
char **str;
int i;
int j;
int temp;
i = 0;
j = 0;
temp = -1;
if (s == NULL)
return (NULL);
if (!(str = (char**)malloc((how_many(s, c) + 1) * sizeof(str))))
return (NULL);
while (s[i] == c)
i++;
str[j] = (char*)ft_memalloc(size_mallocc(s, c, i) + 1);
str = ta3mir(str, s, c, i);
return (str);
}