-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_atoi.c
37 lines (34 loc) · 1.26 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <hbaddrul@student.42kl.edu.my> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/04/28 14:09:31 by hbaddrul #+# #+# */
/* Updated: 2021/08/26 16:23:09 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <limits.h>
#include "libft.h"
int ft_atoi(const char *str)
{
int sign;
long ret;
ret = 0;
sign = 1;
while (ft_isspace(*str))
++str;
if (*str == '+' || *str == '-')
if (*(str++) == '-')
sign *= -1;
while (ft_isdigit(*str))
{
ret = ret * 10 + sign * (*str++ - '0');
if (ret > INT_MAX)
return (-1);
else if (ret < INT_MIN)
return (0);
}
return ((int)ret);
}