-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
76 lines (67 loc) · 1.69 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
68
69
70
71
72
73
74
75
76
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jjaen-de <jjaen-de@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/21 15:45:26 by jjaen-de #+# #+# */
/* Updated: 2024/09/22 16:29:59 by jjaen-de ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(int c)
{
int is_blank;
is_blank = c == ' ' || c == '\t';
if (c == '\f' || c == '\n' || c == '\r' || c == '\v' || is_blank)
return (1);
return (0);
}
static int ft_power(int base, int exp)
{
int res;
res = 1;
while (exp > 0)
{
res *= base;
exp--;
}
return (res);
}
static int ft_get_number(const char *nptr)
{
int i;
int n;
int res;
int candidate;
i = 0;
n = 0;
res = 0;
while (ft_isdigit(*nptr))
{
n++;
nptr++;
}
while (i < n)
{
candidate = *(nptr - n + i);
res += (candidate - '0') * ft_power(10, n - i - 1);
i++;
}
return (res);
}
int ft_atoi(const char *nptr)
{
int sign;
sign = 1;
while (ft_isspace(*nptr))
nptr++;
if (*nptr == '+' || *nptr == '-')
{
if (*nptr == '-')
sign = -1;
nptr++;
}
return (ft_get_number(nptr) * sign);
}