-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
92 lines (82 loc) · 2.27 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebassi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/11 17:03:28 by ebassi #+# #+# */
/* Updated: 2022/01/28 13:47:44 by ebassi ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static long is_space(char *str);
static long ft_sign(char *str, long int index);
static long find_sign(char *str, long int index);
int ft_atoi(const char *str)
{
long nbr;
long index;
long index2;
int sign;
nbr = 0;
index = is_space(((char *)str));
sign = find_sign(((char *)str), index);
index2 = ft_sign(((char *)str), index) - index;
if (index2 > 1)
return (0);
index += index2;
while (((char *)str)[index] == '-' || ((char *)str)[index] == '+')
index++;
while (((char *)str)[index] >= '0' && ((char *)str)[index] <= '9')
{
nbr = nbr * 10 + ((char *)str)[index] - '0';
index++;
}
if (sign)
return (nbr);
return (nbr * -1);
}
static long find_sign(char *str, long int index)
{
long sign;
sign = 0;
while (str[index] == '+' || str[index] == '-')
{
if (str[index] == '-')
{
sign++;
index++;
}
else
index++;
}
if (sign % 2 == 0)
return (1);
return (0);
}
static long ft_sign(char *str, long int index)
{
long sign;
sign = 0;
while (str[index] == '+' || str[index] == '-')
index++;
return (index);
}
static long is_space(char *str)
{
long index;
index = 0;
while (str[index] != '+' || str[index] != '-')
{
if (str[index] == ' ' || str[index] == '\t')
index++;
else if (str[index] == '\n' || str[index] == '\r')
index++;
else if (str[index] == '\f' || str[index] == '\v')
index++;
else
return (index);
}
return (index);
}