-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_split.c
More file actions
97 lines (86 loc) · 2.02 KB
/
ft_split.c
File metadata and controls
97 lines (86 loc) · 2.02 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: llatrice <llatrice@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 20:56:39 by llatrice #+# #+# */
/* Updated: 2021/12/03 13:23:23 by llatrice ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int get_cnt(char const *s, char c)
{
int i;
int cnt;
cnt = 0;
i = 0;
while (s[i])
{
if ((s[i] != c) && ((s[i + 1] == c) || (s[i + 1] == '\0')))
cnt++;
i++;
}
return (cnt);
}
int get_len(char const *s, char c)
{
int i;
int len;
i = 0;
len = 0;
while (s[i] != c && s[i] != '\0')
{
i++;
len++;
}
return (len);
}
void *get_free(char **massiv, int words)
{
int i;
i = 0;
while (i < words)
{
free(massiv[i]);
i++;
}
free(massiv);
return (NULL);
}
char **get_input(char const *s, int words, char c, char **massiv)
{
int i;
int j;
int len;
i = -1;
while (++i < words)
{
while (*s == c)
s++;
len = get_len(s, c);
massiv[i] = (char *)malloc(sizeof(char) * (len + 1));
if (!(massiv[i]))
return (get_free(massiv, i));
j = 0;
while (j < len)
massiv[i][j++] = *s++;
massiv[i][j] = '\0';
}
massiv[i] = NULL;
return (massiv);
}
char **ft_split(char const *s, char c)
{
char **massiv;
int words;
if (!s)
return (NULL);
words = get_cnt(s, c);
massiv = (char **)malloc(sizeof(char *) * (words + 1));
if (!massiv)
return (NULL);
massiv = get_input(s, words, c, massiv);
return (massiv);
}