-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrules_utils_1.c
More file actions
124 lines (113 loc) · 2.49 KB
/
Copy pathrules_utils_1.c
File metadata and controls
124 lines (113 loc) · 2.49 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rules_utils_1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: admansar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/13 18:10:26 by admansar #+# #+# */
/* Updated: 2023/03/27 16:33:09 by admansar ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
// to switch the first with the second
char **switch_1_2(char **c)
{
char *tmp;
tmp = ft_calloc(sizeof(char), ft_strlen(c[0]) + 1);
ft_strlcpy(tmp, c[0], ft_strlen(c[0]) + 1);
ft_strlcpy(c[0], c[1], ft_strlen(c[1]) + 1);
ft_strlcpy(c[1], tmp, ft_strlen(tmp) + 1);
free(tmp);
return (c);
}
int max(int a, int b)
{
if (a > b)
return (a);
return (b);
}
int ft_strcount(char **c)
{
int i;
i = 0;
while (c[i] && c)
i++;
return (i);
}
char **first_to_first(char **a, char **b)
{
int i;
int j;
char **re;
i = 0;
j = 0;
re = ft_calloc(sizeof(char *), ft_strcount(b) + 2);
while (b[j])
{
re[j + 1] = ft_calloc(ft_strlen(b[j]) + 1, sizeof(char));
ft_strlcpy(re[j + 1], b[j], ft_strlen(b[j]) + 1);
j++;
}
while (b[i])
{
free(b[i]);
b[i] = NULL;
i++;
}
free(b);
b = NULL;
re[0] = ft_calloc(ft_strlen(a[0]) + 1, sizeof(char));
ft_strlcpy(re[0], a[0], ft_strlen(a[0]) + 1);
return (re);
}
//to ecrase the first caractere in the string
char **ecrase_avant(char **c)
{
int i;
char **re;
int h;
i = ft_strcount(c);
re = ft_calloc(sizeof(char *), i);
i = 0;
while (c[i + 1])
{
h = ft_strlen(c[i + 1]) + 1;
re[i] = ft_calloc(h, sizeof(char));
ft_strlcpy(re[i], c[i + 1], h);
i++;
}
i = 0;
while (c[i])
{
free(c[i]);
c[i] = NULL;
i++;
}
free(c);
c = NULL;
return (re);
}
/*
char **first_to_first(char **a, char **b)
{
int i;
int j;
char **re;
i = 0;
j = 0;
while (b[i])
i++;
re = ft_calloc(sizeof(char *), i + 2);
while (j < i)
{
re[j + 1] = b[j];
j++;
}
re[0] = a[0];
re[j + 1] = 0;
if (b == malloc(ft_strcount(b)))
free(b);
return (re);
}
*/