Repository navigation
Expand file tree
/
Copy pathft_split.c
More file actions
88 lines (81 loc) · 1.78 KB
/
Copy pathft_split.c
File metadata and controls
88 lines (81 loc) · 1.78 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrafik <mrafik@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/10 11:12:52 by mrafik #+# #+# */
/* Updated: 2021/11/25 13:29:16 by mrafik ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_size(char *str, char c)
{
int x;
int y;
x = 0;
y = 0;
while (str[x] == c)
x++;
while (str[x] != '\0')
{
if (str[x] == c)
{
y++;
while (str[x] == c)
x++;
}
else
x++;
}
return (++y);
}
static char *ft_cm(char *str, const char *s, char c)
{
int x;
int y;
x = 0;
y = 0;
while (s[x] != '\0')
{
if (s[x] == c)
break ;
x++;
}
str = (char *) malloc ((x + 1) * sizeof(char *));
while (y < x)
{
str[y] = s[y];
y++;
}
str[x] = '\0';
return (str);
}
char **ft_split(char const *s, char c)
{
char **str;
int x;
int y;
x = 0;
y = 0;
if (!s)
return (0);
str = (char **) malloc ((ft_size((char *)s, c) + 1) * sizeof(char *));
if (!str)
return (0);
while (s[y] != '\0')
{
if (s[y] != c)
{
str[x] = ft_cm(str[x], &s[y], c);
x++;
while (s[y] != c && s[y] != '\0')
y++;
}
else
y++;
}
str[x] = 0;
return (str);
}