-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split.c
78 lines (71 loc) · 2.15 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
74
75
76
77
78
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kprytkov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/04/28 19:33:33 by kprytkov #+# #+# */
/* Updated: 2018/04/28 19:33:34 by kprytkov ### ########.fr */
/* */
/* ************************************************************************** */
#include "./includes/fdf.h"
static char *i_will_malloc(char *str)
{
char *word;
int i;
i = 0;
while (str[i] && str[i] != ' ' && str[i] != '\n' && str[i] != '\t')
i++;
if (!(word = (char *)malloc(sizeof(char) * (i + 1))))
return (NULL);
i = 0;
while (str[i] && str[i] != ' ' && str[i] != '\n' && str[i] != '\t')
{
word[i] = str[i];
i++;
}
word[i] = '\0';
return (word);
}
static int i_will_count_ws(char *str)
{
int count;
count = 0;
while (*str)
{
while (*str && (*str == ' ' || *str == '\t' || *str == '\n'))
str++;
if (*str && *str != ' ' && *str != '\t' && *str != '\n')
{
count++;
while (*str && *str != ' ' && *str != '\t' && *str != '\n')
str++;
}
}
return (count);
}
char **ft_split(char *str)
{
char **str_array;
int how_much_words;
int counter;
how_much_words = i_will_count_ws(str);
if (!(str_array = (char **)malloc(sizeof(char*) * (how_much_words + 1))))
return (NULL);
counter = 0;
while (*str)
{
while (*str && (*str == ' ' || *str == '\t' || *str == '\n'))
str++;
if (*str && *str != ' ' && *str != '\t' && *str != '\n')
{
str_array[counter] = i_will_malloc(str);
counter++;
while (*str && *str != ' ' && *str != '\t' && *str != '\n')
str++;
}
}
str_array[counter] = NULL;
return (str_array);
}