-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit
96 lines (88 loc) · 1.47 KB
/
split
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
#include <stdlib.h>
#include <stdio.h>
int words_count(char *str)
{
int i;
int w;
w = 0;
i = 0;
while (str[i] != '\0')
{
while (str[i] != '\0' && (str[i] == '\t' || str[i] == '\n'
|| str[i] == ' '))
i++;
if (str[i] != '\0')
w++;
while (str[i] != '\0' && !(str[i] == '\t' || str[i] == '\n'
|| str[i] == ' '))
i++;
}
w++;
return (w);
}
void create_str(char *str, char **res)
{
int i;
int w;
int l;
w = 0;
i = 0;
while (str[i] != '\0')
{
while (str[i] != '\0' && (str[i] == '\t' || str[i] == '\n'
|| str[i] == ' '))
i++;
if (str[i] != '\0' && w++)
l = 1;
else
res[w] = 0;
while (str[i] != '\0' && !(str[i] == '\t' || str[i] == '\n'
|| str[i] == ' ') && i++)
l++;
if (str[i] != '\0' && l > 1)
{
res[w - 1] = malloc(l * sizeof(char));
}
l = 1;
}
}
int fill_array(char *str, char **res, int i, int w)
{
int l;
l = 0;
while (str[i] != '\0')
{
while (str[i] != '\0' && (str[i] == '\t' || str[i] == '\n'
|| str[i] == ' '))
{
if (l > 0)
res[w - 1][l] = '\0';
i++;
l = 0;
}
while (str[i] != '\0' && !(str[i] == '\t'
|| str[i] == '\n' || str[i] == ' '))
{
res[w][l] = str[i];
printf("%d ",i);
i++;
l++;
}
w++;
printf("%d",w);
}
return (w);
}
char **ft_split_whitespaces(char *str)
{
char **res;
int w;
int i;
res = malloc(words_count(str) * sizeof(char*));
w = 0;
i = 0;
create_str(str, res);
w = fill_array(str, res, i, w);
res[w] = 0;
return (res);
}