-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
42 lines (39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aelsayed <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/22 12:26:09 by aelsayed #+# #+# */
/* Updated: 2024/11/04 16:10:13 by aelsayed ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(char const *str)
{
unsigned int i;
int sign;
long n;
long min;
long max;
i = 0;
sign = 1;
n = 0;
min = -9223372036854775807 - 1;
max = 9223372036854775807;
while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32)
i++;
if (str[i] == '-' || str[i] == '+')
if (str[i++] == '-')
sign = -1;
while (str[i] <= '9' && str[i] >= '0')
{
if (sign == 1 && (n > (max - (str[i] - '0')) / 10))
return (-1);
else if (sign == -1 && (n < (min + (str[i] - '0')) / 10))
return (0);
n = n * 10 + (str[i++] - '0');
}
return (n * sign);
}