-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi_not_used.c
60 lines (53 loc) · 1.55 KB
/
ft_atoi_not_used.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: eelasam <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/10 18:11:12 by eelasam #+# #+# */
/* Updated: 2023/04/10 17:36:09 by eelasam ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int s;
long r;
int valid;
r = 0;
s = 1;
valid = 1;
while (*str == ' ' || (*str >= 9 && *str <= 13))
str++;
if (*str == '-' || *str == '+')
{
if (*str == '-')
s = -s;
str++;
if (*str == '-' || *str == '+')
{
//ft_printf("++--");
valid = 0;
}
}
while (*str != '\0' && (*str >= '0' && *str <= '9'))
{
r = (r * 10) + (*str - '0');
str++;
}
if ((r * s > 2147483647) || (r * s < -2147483648))
{
// ft_printf ("MIN/MAX:");
valid = 0;
}
if (*str != '\0' && *str != ' ' && valid)
{
// ft_printf("char");
valid = 0;
}
if (valid)
return ((int)(r * s));
else
return (0);
}