-
Notifications
You must be signed in to change notification settings - Fork 0
/
string1.c
107 lines (97 loc) · 2.12 KB
/
string1.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* string1.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebennamr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/04 18:07:44 by ebennamr #+# #+# */
/* Updated: 2023/01/04 20:14:27 by ebennamr ### ########.fr */
/* */
/* ************************************************************************** */
#include "pushswap.h"
size_t ft_strlen( char *str)
{
size_t len;
len = 0;
while (str[len] != '\0')
len++;
return (len);
}
char *ft_substr(char *s, unsigned int start, size_t len)
{
size_t j;
char *p;
if (!s)
return (NULL);
if (len <= ft_strlen(s + start))
p = (char *)(malloc(len + 1));
else
p = (char *)(malloc(ft_strlen(s + start) + 1));
j = 0;
if (!p || !s)
{
return (0);
}
while (s[start] && j < len && start < ft_strlen(s))
{
p[j] = s[start];
j++;
start++;
}
p[j] = '\0';
return (p);
}
char *ft_strjoin(char *src1, char *src2)
{
int len;
int count;
char *pt;
if (!src1 || !src2)
return (0);
len = ft_strlen(src1) + ft_strlen(src2);
count = 0;
pt = malloc(len + 1);
if (!pt)
return (0);
while (src1[count])
{
*(pt++) = src1[count];
count++;
}
count = 0;
while (src2[count])
{
*(pt++) = src2[count];
count++;
}
*pt = '\0';
return (pt - len);
}
void ft_error(char *err)
{
write(2, err, ft_strlen(err));
exit(1);
}
long ft_atoi(char *str)
{
long i;
long mult;
long nb;
mult = 1;
nb = 0;
i = 0;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
mult *= -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
nb = (nb * 10) + (str[i] - '0');
i++;
}
nb *= mult;
return (nb);
}