-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
39 lines (36 loc) · 1.41 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apimikov <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/03 13:13:34 by apimikov #+# #+# */
/* Updated: 2023/11/15 07:11:41 by apimikov ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int sign;
long long answ;
long long llong_max;
llong_max = ((unsigned long long)(-1)) / 2;
sign = 1;
answ = 0;
while ((8 < *str && *str < 14) || *str == ' ')
str++;
if (*str == '-' || *str == '+')
if (*str++ == '-')
sign = -1;
while ('0' <= *str && *str <= '9')
{
if (answ > llong_max / 10)
return ((sign > 0) * (-1));
answ = answ * 10;
if (answ > llong_max - (*str - '0'))
return ((sign > 0) * (-1));
answ = answ + ((*str++) - '0');
}
return ((int)(sign * answ));
}