-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
40 lines (38 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hshawand <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/04 16:06:59 by hshawand #+# #+# */
/* Updated: 2019/04/08 17:09:51 by hshawand ### ########.fr */
/* */
/* ************************************************************************** */
int ft_atoi(const char *str)
{
long int result;
int sign;
result = 0;
sign = 1;
while (*str == ' ' || *str == '\n' || *str == '\t' ||
*str == '\v' || *str == '\f' || *str == '\r')
{
str = str + 1;
}
if (*str == '-')
{
sign = -1;
str = str + 1;
}
else if (*str == '+')
str = str + 1;
while (*str >= '0' && *str <= '9')
{
if (sign * result > sign * (result * 10 + sign * (*str)))
return (sign == 1 ? -1 : 0);
result = result * 10 + sign * (*str - '0');
str = str + 1;
}
return ((int)result);
}