-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
47 lines (43 loc) · 1.39 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ssoares- <ssoares-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/26 05:18:59 by ssoares- #+# #+# */
/* Updated: 2022/07/23 21:43:45 by ssoares- ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char c)
{
return ((c == ' '
|| c == '\t'
|| c == '\r'
|| c == '\v'
|| c == '\n'
|| c == '\f'));
}
int ft_atoi(const char *nptr)
{
int result;
int signal;
result = 0;
signal = 0;
while (ft_isspace(*nptr))
nptr++;
if (*nptr == '-' || *nptr == '+')
{
if (*nptr == '-')
signal = 1;
nptr++;
}
else if (*nptr == '+')
nptr++;
while (ft_isdigit(*nptr))
result = (result * 10) + (*nptr++ - 48);
if (signal)
return (result * -1);
return (result);
}