-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmy_spliter.c
129 lines (118 loc) · 2.19 KB
/
my_spliter.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* my_spliter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: admansar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/14 12:02:08 by admansar #+# #+# */
/* Updated: 2022/12/24 15:49:54 by admansar ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int space_counter(char *s)
{
int j;
int i;
j = 0;
i = 0;
while (s[i])
{
if (s[i] == ' ')
j++;
i++;
}
j++;
return (j);
}
char *cutter(char *s)
{
int i;
int j;
char *tmp;
i = 0;
j = 0;
tmp = ft_calloc(ft_strlen(s) + 1, sizeof(char));
while (s[i])
{
while (s[i] == ' ' && s[i + 1] == ' ')
{
i++;
}
tmp[j] = s[i];
j++;
i++;
}
return (tmp);
}
char *protecter(char *str, int i)
{
int j;
int k;
char *re;
i = 0;
while (str[i] == ' ')
i++;
j = i;
while (str[i])
i++;
i--;
while (str[i] == ' ')
i--;
k = i + 1;
re = ft_calloc(i + 2, sizeof(char));
i = j;
j = 0;
while (str[i])
{
re[j++] = str[i++];
if (i == k)
break ;
}
free(str);
return (re);
}
char **norm(char *s, char **re)
{
int i;
int j;
int k;
i = 0;
j = 0;
k = 0;
while (s[i])
{
if (s[i] == ' ' && i > 0)
{
k = 0;
i++;
j++;
}
re[j][k] = s[i];
i++;
k++;
}
return (re);
}
char **my_spliter(char *s, int i, int j, int k)
{
char **re;
s = cutter(s);
s = protecter(s, 0);
re = ft_calloc(sizeof(char *), (space_counter(s) + 1));
while (s[i])
{
if (s[i] == ' ' && i > 0)
{
re[j] = ft_calloc(sizeof(char), (i - k + 1));
j++;
k = i;
k++;
}
i++;
}
re[j] = ft_calloc(sizeof(char), (i - k + 1));
re = norm(s, re);
free(s);
return (re);
}