-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
44 lines (40 loc) · 1.33 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gmachado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/04/07 18:52:55 by gmachado #+# #+# */
/* Updated: 2022/04/21 00:10:25 by gmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char c)
{
return (c == ' ' || (c >= 9 && c <= 13));
}
int ft_atoi(const char *nptr)
{
int is_negative;
int result;
result = 0;
is_negative = 0;
while (ft_isspace(*nptr))
nptr++;
if (*nptr == '-')
{
is_negative = 1;
nptr++;
}
else if (*nptr == '+')
nptr++;
while (*nptr >= '0' && *nptr <= '9')
{
result = 10 * result - (int)(*nptr - '0');
nptr++;
}
if (is_negative)
return (result);
return (-result);
}