-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathft_atoi.c
49 lines (45 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: daelee <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/01 14:08:11 by daelee #+# #+# */
/* Updated: 2020/04/09 18:10:16 by daelee ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_isspace(char c)
{
if (c == ' ' || c == '\n' || c == '\t' ||
c == '\v' || c == '\f' || c == '\r')
return (1);
else
return (0);
}
int ft_atoi(const char *str)
{
long nbr;
long sign;
size_t i;
nbr = 0;
sign = 1;
i = 0;
while ((str[i] != '\0') && ft_isspace(str[i]) == 1)
i++;
if (str[i] == '-')
sign = -1;
if ((str[i] == '-') || (str[i] == '+'))
i++;
while ((str[i] != '\0') && ('0' <= str[i]) && (str[i] <= '9'))
{
nbr = (nbr * 10) + (str[i] - '0');
if (nbr > 2147483647 && sign == 1)
return (-1);
if (nbr > 2147483648 && sign == -1)
return (0);
i++;
}
return (sign * nbr);
}