-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
65 lines (58 loc) · 1.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvon-nag <fvon-nag@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/25 08:12:00 by fvon-nag #+# #+# */
/* Updated: 2022/12/14 17:08:51 by fvon-nag ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int skip(const char *nptr, int *isneg, int i)
{
int sign;
sign = 0;
while (nptr[i] != '\0' && (nptr[i] == 32 || nptr[i] == 12
|| nptr[i] == 9 || nptr[i] == 10 || nptr[i] == 11 || nptr[i] == 13))
i++;
while (nptr[i] != '\0' && nptr[i] == '-' && sign == 0)
{
*isneg = 1;
i++;
sign++;
}
while (nptr[i] != '\0' && nptr[i] == '+' && sign == 0)
{
sign++;
i++;
}
return (i);
}
static int digitize(const char *nptr, int i)
{
int num;
num = 0;
while (nptr[i] != '\0' && ft_isdigit(nptr[i]))
{
num *= 10;
num += nptr[i] - '0';
i++;
}
return (num);
}
int ft_atoi(const char *nptr)
{
int i;
int num;
int isneg;
isneg = 0;
num = 0;
i = 0;
num = digitize(nptr, skip(nptr, &isneg, i));
if (isneg == 0)
return (num);
else
return (num * -1);
}