-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_splitspace_bonus.c
61 lines (56 loc) · 1.54 KB
/
ft_splitspace_bonus.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_splitspace_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lrocca <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/11 15:46:11 by lrocca #+# #+# */
/* Updated: 2021/05/11 15:48:47 by lrocca ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_wcount(const char *s)
{
int count;
count = 0;
while (*s)
{
if (!ft_isspace(*s))
{
count++;
while (*s && !ft_isspace(*s))
s++;
}
else
s++;
}
return (count);
}
char **ft_splitspace(const char *s)
{
size_t i;
char *start;
char **array;
array = malloc((ft_wcount(s) + 1) * sizeof(char *));
if (!s || !array)
return (NULL);
i = 0;
while (*s)
{
if (!ft_isspace(*s))
{
start = (char *)s;
while (*s && !ft_isspace(*s))
s++;
array[i] = malloc((s - start) + 1);
if (!array[i])
return (NULL);
ft_strlcpy(array[i++], start, s - start + 1);
}
else
s++;
}
array[i] = 0;
return (array);
}