-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_bonus.c
More file actions
114 lines (104 loc) · 2.28 KB
/
utils_bonus.c
File metadata and controls
114 lines (104 loc) · 2.28 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: uyilmaz <uyilmaz@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/27 10:03:47 by uyilmaz #+# #+# */
/* Updated: 2023/03/06 23:51:57 by uyilmaz ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap_bonus.h"
void ft_putstr(char *str)
{
int i;
if (!str)
return ;
i = 0;
while (str[i])
{
write (1, &str[i], 1);
i++;
}
}
int ft_strncmp(const char *s1, const char *s2, size_t n)
{
while (*s1 && *s2 && n)
{
if (*s1 != *s2)
return (0);
s1++;
s2++;
n--;
}
if (n == 0)
{
s1--;
s2--;
}
return (1);
}
char *ft_strjoin_v3(char *s1, char *s2)
{
size_t i;
size_t j;
char *result;
if (!s1)
{
s1 = malloc(sizeof(char) * 1);
s1[0] = '\0';
}
if (!s2)
return (0);
result = malloc(sizeof(char) * (ft_strlen(s1) + ft_strlen(s2) + 2));
if (!result)
return (0);
i = -1;
while (s1[++i])
result[i] = s1[i];
j = 0;
while (s2[j])
result[i++] = s2[j++];
free(s1);
result[i++] = ' ';
result[i] = '\0';
return (result);
}
int ft_atoi(const char *str)
{
long result;
int sign;
int i;
result = 0;
sign = 1;
i = 0;
while ((str[i] >= 9 && str[i] <= 13) || (str[i] == 32))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i++] == '-')
sign *= -1;
}
while (str[i] >= '0' && str[i] <= '9' && str[i])
{
result = result * 10 + (str[i] - 48) * sign;
if (result > 2147483647 || result < -2147483648)
{
print_error("Error\n");
exit (1);
}
i++;
}
return ((int)result);
}
char *all_in_one(int count, char **values)
{
char *result;
int i;
i = 0;
result = NULL;
while (i < count)
result = ft_strjoin_v3(result, values[i++]);
return (result);
}