-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
53 lines (48 loc) · 1.58 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: younhwan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/06 19:53:11 by younhwan #+# #+# */
/* Updated: 2022/07/14 17:06:00 by younhwan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "limits.h"
int ft_atoi(const char *str);
static int ft_isspace(char c);
int ft_atoi(const char *str)
{
unsigned long long nbr;
int sign;
size_t i;
nbr = 0;
sign = 1;
i = 0;
while (str[i] && ft_isspace(str[i]))
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
sign = -1;
i++;
}
while (str[i] && ('0' <= str[i] && str[i] <= '9'))
{
nbr = 10 * nbr + (str[i] - '0');
if (sign == 1 && __LONG_LONG_MAX__ < nbr)
return (-1);
if (sign == -1 && (unsigned long long) __LONG_LONG_MAX__ + 1 < nbr)
return (0);
i++;
}
return ((int) nbr * sign);
}
static int ft_isspace(char c)
{
if ((9 <= c && c <= 13) || c == 32)
return (1);
return (0);
}