-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
50 lines (46 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bcaffere <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/05 17:02:56 by bcaffere #+# #+# */
/* Updated: 2021/05/05 17:38:03 by bcaffere ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int number(long x, int sign)
{
if (sign < 0 && x <= 2147483648)
return (sign * x);
if (sign > 0 && x <= 2147483647)
return (sign * x);
return (0);
}
int ft_atoi(const char *s)
{
int sign;
long x;
x = 0;
sign = 1;
while ((*s > 8 && *s < 14) || *s == 32)
s++;
if (*s == '-' || *s == '+')
{
if (*s == '-')
sign = -sign;
s++;
}
while (*s)
{
if (*s >= '0' && *s <= '9')
{
x = x * 10 + *s - '0';
s++;
}
else
return (number(x, sign));
}
return (number(x, sign));
}