-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_whitespaces.c
102 lines (93 loc) · 2.11 KB
/
ft_whitespaces.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_whitespaces.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lbopp <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/07 12:23:02 by lbopp #+# #+# */
/* Updated: 2017/02/07 12:31:32 by lbopp ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_word(char const *s)
{
int i;
int word;
i = 0;
word = 0;
while (s[i])
{
if (s[i] && !ft_isspace(s[i]))
word++;
while (s[i] && !ft_isspace(s[i]))
i++;
s[i] ? i++ : 0;
}
return (word);
}
static char **ft_fill(char *s, char **array)
{
int i;
int letter;
int word;
i = 0;
word = 0;
while (s[i])
{
letter = 0;
while (s[i] && !ft_isspace(s[i]))
{
array[word][letter] = s[i];
letter++;
i++;
}
while (s[i] && ft_isspace(s[i]))
i++;
word++;
}
free(s);
return (array);
}
static char **ft_letter(char const *s, char **array)
{
int i;
int letter;
int word;
i = 0;
word = 0;
while (s[i])
{
letter = 0;
while (s[i] && !ft_isspace(s[i]))
{
letter++;
i++;
}
while (s[i] && ft_isspace(s[i]))
i++;
if (!(array[word] = (char*)ft_memalloc(letter + 1)))
return (NULL);
word++;
}
return (array);
}
char **ft_whitespaces(char const *s)
{
int i;
int word;
char **array;
char *new;
i = 0;
word = 0;
if (!(s))
return (NULL);
if (!(new = ft_strtrim(s)))
return (0);
word = ft_word(new);
if (!(array = (char**)ft_memalloc(sizeof(char*) * (word + 1))))
return (0);
if (!(array = ft_letter(new, array)))
return (0);
return (ft_fill(new, array));
}