-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
50 lines (46 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jcampagn <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/03 20:49:37 by jcampagn #+# #+# */
/* Updated: 2021/11/28 11:56:28 by jcampagn ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_outvalue(int neg)
{
if (neg < 0)
return (0);
else
return (-1);
}
int ft_atoi(const char *str)
{
int i;
unsigned long long value;
int neg;
i = 0;
value = 0;
neg = 1;
while (str[i] && (str[i] == ' ' || str[i] == '\f' || str[i] == '\n'
|| str[i] == '\r' || str[i] == '\t' || str[i] == '\v'))
i++;
if (str[i] == '-')
{
neg *= -1;
i++;
}
else if (str[i] == '+')
i++;
while (str[i] && (str[i] >= '0' && str[i] <= '9'))
{
if (value > value * 10 + str[i] + '0')
return (ft_outvalue(neg));
value = value * 10 + str[i] - '0';
i++;
}
return (neg * value);
}