-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
73 lines (66 loc) · 1.77 KB
/
ft_split.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: melkholy <melkholy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/15 17:32:19 by melkholy #+# #+# */
/* Updated: 2022/05/12 21:46:49 by melkholy ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_wcount(const char *str, char c)
{
size_t found;
found = 0;
while (*str)
{
if (*str != c)
{
while (*str != c && *str)
str ++;
found ++;
}
else
str ++;
}
return (found);
}
char *ft_divide(const char *str, char c)
{
char *dev;
size_t count;
count = 0;
while (str[count] != c && str[count])
count ++;
dev = (char *)malloc(count + 1);
ft_strlcpy(dev, str, count + 1);
return (dev);
}
char **ft_split(char const *s, char c)
{
size_t count;
char **split;
size_t index;
size_t rep;
count = 0;
index = 0;
if (!s)
return (NULL);
rep = ft_wcount(s, c);
split = (char **)malloc((rep + 1) * sizeof(char *));
if (!split)
return (NULL);
while (index < rep)
{
split[index] = ft_divide(s + count, c);
count += ft_strlen(split[index]) + 1;
if (*(split[index]))
index ++;
else
free(split[index]);
}
split[index] = NULL;
return (split);
}