-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
38 lines (36 loc) · 1.25 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ylagtab <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/30 16:30:03 by ylagtab #+# #+# */
/* Updated: 2019/04/20 01:59:21 by ylagtab ### ########.fr */
/* */
/* ************************************************************************** */
int ft_atoi(const char *s)
{
long res;
int sign;
res = 0;
sign = 1;
while (*s == ' ' || (*s >= 9 && *s <= 13))
s++;
if (*s == '-')
sign = -1;
if (*s == '+' || *s == '-')
s++;
while (*s && *s >= '0' && *s <= '9')
{
res = res * 10;
res += *s - '0';
s++;
}
res *= sign;
if (sign == 1 && res < 0)
return (-1);
if (sign == -1 && res > 0)
return (0);
return (res);
}