-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
67 lines (60 loc) · 1.57 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
65
66
67
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asoler <asoler@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/10 18:52:29 by asoler #+# #+# */
/* Updated: 2022/04/18 15:35:13 by asoler ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int power(const char n, int len)
{
int number;
number = n - 48;
if (len == 0)
return (number);
while (len > 0)
{
number *= 10;
len--;
}
return (number);
}
static int ft_result(const char *n, int len)
{
int result;
result = 0;
while (len > 0)
{
len--;
result += power(*n, len);
n++;
}
return (result);
}
int ft_atoi(const char *nptr)
{
int i;
long int result;
int b;
i = 0;
b = 0;
while ((*nptr >= 9 && *nptr <= 13) || *nptr == 32)
nptr++;
if (*nptr == '-')
{
b = 1;
nptr++;
}
else if (*nptr == '+')
nptr++;
while (nptr[i] >= '0' && nptr[i] <= '9')
i++;
result = ft_result(nptr, i);
if (b)
result *= -1;
return ((int)result);
}