-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_split_whitespaces.c
87 lines (80 loc) · 1.8 KB
/
ft_split_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_split_whitespaces.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bel-bouz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/08/05 01:56:28 by bel-bouz #+# #+# */
/* Updated: 2018/08/06 04:27:03 by bel-bouz ### ########.fr */
/* */
/* ************************************************************************** */
#include "swap.h"
int count_word(char *str)
{
int i;
int l;
int cn;
if (str[0] == '\0')
return (0);
i = 0;
l = 0;
cn = 0;
while (str[cn])
{
if (str[cn] == ' ' || str[cn] == '\n'
|| str[cn] == '\t' || str[cn] == '\0')
{
l = 0;
}
else
{
if (l == 0)
i++;
l = 1;
}
cn++;
}
return (i);
}
char *cat_word(char *str, int *n)
{
char *word;
int i;
int start;
int end;
i = *n;
while (str[i] <= ' ')
i++;
start = i;
while (str[i] > ' ')
i++;
end = i;
*n = i;
word = (char*)malloc(sizeof(char) * end - start);
i = 0;
while (start < end)
{
word[i] = str[start];
start++;
i++;
}
word[i] = '\0';
return (word);
}
char **ft_split_whitespaces(char *str)
{
char **res;
int count;
int i;
res = (char**)malloc(sizeof(char*) * count_word(str) + 1);
count = 0;
i = 0;
while (count < count_word(str))
{
res[count] = cat_word(str, &i);
count++;
}
res[count] = 0;
return (res);
}