-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_function.c
106 lines (96 loc) Β· 2.18 KB
/
utils_function.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utils_function.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahajji <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/13 11:58:23 by ahajji #+# #+# */
/* Updated: 2023/02/02 17:30:17 by ahajji ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
int ft_strlen(char *s)
{
int i;
i = 0;
while (s != NULL && s[i])
{
i++;
}
return (i);
}
char *ft_substr(char *s, unsigned int start, int len)
{
size_t i;
size_t j;
char *p;
i = 0;
j = 0;
if (!s)
return (0);
if (len == 0 || start >= ft_strlen(s))
{
p = malloc(1);
p[0] = '\0';
return (p);
}
p = malloc(len + 1);
if (!p)
return (0);
while (s[start] && j < len)
p[j++] = s[start++];
p[j] = '\0';
return (p);
}
char *ft_strjoin(char *s1, char *s2)
{
int j;
int count;
char *p;
j = 0;
count = 0;
p = malloc(ft_strlen(s1) + ft_strlen(s2) + 2);
if (!p)
return (0);
while (count < ft_strlen(s1))
{
p[count] = s1[count];
count++;
}
while (count < ft_strlen(s1) + ft_strlen(s2))
p[count++] = s2[j++];
p[count++] = ' ';
p[count] = '\0';
free(s1);
return (p);
}
int long ft_atoi(const char *str)
{
int i;
int st;
int long result;
st = 1;
i = 0;
result = 0;
while ((str[i] == 32 || str[i] == '\t' || str[i] == '\n' || str[i] == '\v'
|| str[i] == '\f' || str[i] == '\r') && str[i])
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
st *= -1;
i++;
}
while (str[i] >= 48 && str[i] <= 57)
{
result = result * 10 + str[i] - 48;
i++;
}
return (result * st);
}
void print_error(void)
{
write(2, "error", 5);
exit(1);
}