-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
54 lines (49 loc) · 1.48 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hkonte <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/03 17:58:21 by hkonte #+# #+# */
/* Updated: 2024/11/29 13:15:20 by hkonte ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_is_sign(char c)
{
return (c == '+' || c == '-');
}
static int ft_is_space(char c)
{
if (c == ' ' || c == '\n' || c == '\t')
return (1);
return (c == '\r' || c == '\v' || c == '\f');
}
int ft_atoi(const char *nptr)
{
int result;
int i;
int p;
int val;
result = 0;
i = 0;
p = 1;
while (nptr[i] != '\0' && ft_is_space(nptr[i]))
i++;
if (ft_is_sign(nptr[i]))
{
if (nptr[i] == '-')
p *= -1;
i++;
}
while (nptr[i] != '\0')
{
val = (int)(nptr[i] - '0');
if (val < 0 || val > 9)
return (p * result);
result = result * 10 + val;
i++;
}
return (p * result);
}