-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
80 lines (73 loc) · 1.88 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mdeville <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/07/06 09:51:42 by mdeville #+# #+# */
/* Updated: 2017/07/07 12:00:08 by mdeville ### ########.fr */
/* */
/* ************************************************************************** */
int ft_strlen(char *str)
{
int i;
i = 0;
while (str[i])
{
i++;
}
return (i);
}
int is_whitespace(char c)
{
return (c == ' ' || c == '\t' || c == '\n' || c == '\12');
}
int is_digit(char c)
{
return (c >= '0' && c <= '9');
}
int test_char(char *c, int *begin, int *stop, int *is_negative)
{
if ((is_whitespace(*c) || *c == '+') && *begin == 0)
{
return (0);
}
if (*c == '-' && *begin == 0 && is_digit(*(c + 1)))
{
*is_negative = 1;
return (0);
}
if (!(is_digit(*c)))
{
*stop = 1;
return (0);
}
if (is_digit(*c))
{
*begin = 1;
return (*c - '0');
}
return (0);
}
int ft_atoi(char *str)
{
int i;
int result;
int begin_stop[2];
int is_negative;
int tmp;
begin_stop[0] = 0;
is_negative = 0;
begin_stop[1] = 0;
i = 0;
result = 0;
while (i < ft_strlen(str) && begin_stop[1] == 0)
{
tmp = test_char(str + i, begin_stop, begin_stop + 1, &is_negative);
result = (begin_stop[1]) ? result : result * 10 + tmp;
i++;
}
result = (is_negative) ? -result : result;
return (result);
}