-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
111 lines (101 loc) · 2.2 KB
/
ft_split.c
File metadata and controls
111 lines (101 loc) · 2.2 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
108
109
110
111
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lyandriy <lyandriy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/23 20:04:18 by lyandriy #+# #+# */
/* Updated: 2022/10/02 14:41:23 by lyandriy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static char *ft_cpy(char *dst, const char *src, int n)
{
int i;
i = 0;
if (src == 0 || dst == 0)
return (0);
while (i < n)
{
dst[i] = ((char *)src)[i];
i++;
}
dst[i] = '\0';
return (dst);
}
static int ft_count(const char *s, char c)
{
int i;
i = 0;
while (s[i] != c && s[i] != '\0')
i++;
return (i);
}
static void **ft_free(char **ptr, int j)
{
if (j == 0)
{
free(ptr);
return (NULL);
}
while (j > -1)
{
free(ptr[j]);
j--;
}
free(ptr);
return (NULL);
}
static char **ft_split_0(char **ptr, const char *s, char c)
{
int i;
int j;
int count;
i = 0;
j = 0;
while (s[i] != '\0')
{
if (s[i] == c)
i++;
if (s[i] != c && s[i] != '\0')
{
count = ft_count(&s[i], c);
ptr[j] = malloc ((count + 1) * sizeof (char));
if (!ptr[j])
return ((char **)ft_free(ptr, j - 1));
ptr[j] = ft_cpy(ptr[j], &s[i], count);
i = i + count;
j++;
}
}
ptr[j] = (NULL);
return (ptr);
}
char **ft_split(char const *s, char c)
{
char **ptr;
int i;
int count;
int k;
i = 0;
k = 0;
if (!s)
return (NULL);
while (s[i] != '\0')
{
if (s[i] == c)
i++;
if (s[i] != c && s[i] != '\0')
{
count = ft_count(&s[i], c);
i = i + count;
k++;
}
}
ptr = (char **) malloc((k + 1) * sizeof (char *));
if (!ptr)
return (NULL);
ft_split_0(ptr, s, c);
return (ptr);
}