-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
65 lines (60 loc) · 1.64 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
54
55
56
57
58
59
60
61
62
63
64
65
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lkuenane <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/06/02 11:09:33 by lkuenane #+# #+# */
/* Updated: 2017/06/08 17:09:57 by lkuenane ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int pre_atoi(const char *str)
{
int i;
int positive;
int negative;
i = 0;
positive = 0;
negative = 0;
while (str[i])
{
if (str[i] == '+')
positive++;
if (str[i] == '-')
negative++;
i++;
}
if (positive >= 2)
return (0);
if (positive > 0 && negative > 0)
return (0);
else
return (1);
}
int ft_atoi(const char *str)
{
int i;
int neg;
long long res;
i = 0;
neg = 1;
res = 0;
if (pre_atoi(str) == 0)
return (0);
while (str[i] == ' ' || str[i] == '\t' || str[i] == '\v' || str[i] == '+'
|| str[i] == '\f' || str[i] == '\r' || str[i] == '\n')
i++;
if (str[i] == '-')
{
neg = -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
res = res * 10 + (str[i] - '0');
i++;
}
return (res * neg);
}