-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
42 lines (39 loc) · 1.46 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: enikel <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/05/21 09:07:57 by enikel #+# #+# */
/* Updated: 2018/06/14 09:57:21 by enikel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
unsigned long int i;
int c;
int neg;
c = 0;
i = 0;
while ((str[c] == '\n') || (str[c] == '\t') || (str[c] == '\r') ||
(str[c] == '\f') || (str[c] == ' ') || (str[c] == '\v'))
c++;
if (str[c] == '+' && ft_isdigit(str[c + 1]))
c++;
if (str[c] == '-' && ft_isdigit(str[c + 1]))
{
neg = 1;
c++;
}
while (str[c] >= '0' && str[c] <= '9')
i = i * 10 + (str[c++] - 48);
if (i > 9223372036854775807 && neg == 1)
return (0);
if (i > 9223372036854775807)
return (-1);
if (neg == 1)
return (i * -1);
return (i);
}