-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
64 lines (56 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: julmuntz <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/17 17:01:44 by julmuntz #+# #+# */
/* Updated: 2022/06/02 22:19:07 by julmuntz ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int i[2];
long result;
i[0] = 0;
i[1] = 1;
result = 0;
while ((str[i[0]] == ' ') || (str[i[0]] >= '\t' && str[i[0]] <= '\r'))
i[0]++;
if (str[i[0]] == '-')
{
i[1] *= -1;
i[0]++;
}
else if (str[i[0]] == '+')
i[0]++;
while (str[i[0]] >= '0' && str[i[0]] <= '9')
{
result = result * 10 + str[i[0]] - 48;
if ((unsigned long)result > LONG_MAX && i[1] == 1)
return (-1);
if ((unsigned long)result > LONG_MAX && i[1] == -1)
return (0);
i[0]++;
}
return (result * i[1]);
}
/*
#include <stdio.h>
int main(int arc, char **arv)
{
int var;
if (arc == 2)
{
puts("\n- atoi");
var = atoi(arv[1]);
printf("%d\n", var);
puts("\n- ft_atoi");
var = ft_atoi(arv[1]);
printf("%d\n", var);
perror("\nStatus");
}
}
*/