-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
56 lines (51 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jsprouts <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/09/21 18:13:32 by jsprouts #+# #+# */
/* Updated: 2019/09/21 21:12:53 by jsprouts ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_check(const char *str, int i)
{
int nb;
int j;
if (i == 1)
j = -1;
else
j = 0;
nb = 0;
while (*str >= '0' && *str <= '9')
{
if (j == 0)
nb = nb * 10 + (*str - '0');
else if (i)
{
nb = -1 * (*str - '0');
i = 0;
}
else
nb = nb * 10 - (*str - '0');
str++;
}
return (nb);
}
int ft_atoi(const char *str)
{
int i;
i = 0;
while ((*str >= '\t' && *str <= '\r') || *str == ' ')
str++;
if (*str == '+')
str++;
else if (*str == '-')
{
str++;
i = 1;
}
return (ft_check(str, i));
}