-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
68 lines (62 loc) · 1.52 KB
/
Copy pathft_atoi.c
File metadata and controls
68 lines (62 loc) · 1.52 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
63
64
65
66
67
68
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mrafik <mrafik@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/11 15:13:56 by mrafik #+# #+# */
/* Updated: 2021/11/23 21:46:03 by mrafik ### ########.fr */
/* */
/* ************************************************************************** */
#include"libft.h"
static int long_handle(int x, int i, int j)
{
if (i >= 19 && j > 0)
return (-1);
if (i >= 19 && j < 0)
return (0);
else
return (x);
}
static int f(char a)
{
int i;
i = 0;
if (a == ' '
|| a == '\t'
|| a == '\n'
|| a == '\v'
|| a == '\f'
|| a == '\r' )
{
return (0);
}
return (1);
}
int ft_atoi(const char *str)
{
int i;
int j;
int x;
i = 0;
j = 1;
x = 0;
while (f(str[i]) == 0)
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
{
j = j * (-1);
}
i++;
}
while (str[i] >= '0' && str[i] <= '9' )
{
x = x * 10 + (str[i] - '0');
i++;
}
x = long_handle(x, i, j);
return (x * j);
}