-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
66 lines (59 loc) · 1.56 KB
/
ft_atoi.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpicoli- < lpicoli-@student.42porto.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/09 12:43:55 by lpicoli- #+# #+# */
/* Updated: 2022/11/16 08:25:46 by lpicoli- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isnegative(char c)
{
if (c == '-')
return (1);
return (0);
}
int ft_ispositive(char c)
{
if (c == '+')
return (1);
return (0);
}
int ft_isspace(char c)
{
if (c == 32 || (c >= 9 && c <= 13))
return (1);
return (0);
}
int ft_atoi(const char *nptr)
{
int r;
int i;
int signal;
r = 0;
i = 0;
signal = 1;
while (ft_isspace(nptr[i]))
i++;
if (ft_isnegative(nptr[i]) || ft_ispositive(nptr[i]))
{
if (ft_isnegative(nptr[i]))
signal *= -1;
i++;
}
while (nptr[i] >= '0' && nptr[i] <= '9')
{
r *= 10;
r += nptr[i] - '0';
i++;
}
return (r * signal);
}
/*int main()
{
printf("%d\n", atoi(" +a1b2a"));
printf("%d\n", ft_atoi(" +a1b2a"));
}*/