-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
62 lines (58 loc) · 1.68 KB
/
ft_atoi.c
File metadata and controls
62 lines (58 loc) · 1.68 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aycami <aycami@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/07 14:54:26 by aycami #+# #+# */
/* Updated: 2024/10/23 02:03:27 by aycami ### ########.fr */
/* */
/* ************************************************************************** */
static int ft_check(unsigned long long result, const char *str, int sign,
int i)
{
if (result > 922337203685477580 || (result == 922337203685477580 && (str[i]
- '0') > 7))
{
if (sign == 1)
return (-1);
else
return (0);
}
else
return (1);
}
static int ft_white_space(int i, const char *str)
{
while (str[i] == 32 || (str[i] >= 9 && str[i] <= 13))
i++;
return (i);
}
int ft_atoi(const char *str)
{
int i;
long long result;
int sign;
int re;
i = 0;
result = 0;
sign = 1;
re = 0;
i += ft_white_space(i, str);
if (str[i] == '+')
i++;
else if (str[i] == '-')
{
sign = -1;
i++;
}
while (str[i] >= 48 && str[i] <= 57)
{
re = ft_check(result, str, sign, i);
if (re == -1 || re == 0)
return (re);
result = ((result * 10) + (str[i++] - 48));
}
return ((int)(result * sign));
}