-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
57 lines (52 loc) · 1.74 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyungkim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/21 05:42:41 by kyungkim #+# #+# */
/* Updated: 2024/11/21 09:03:55 by kyungkim ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int str_int(const char *nptr, unsigned int sum)
{
if (!('0' <= *nptr && *nptr <= '9'))
return (sum);
sum *= 10;
sum += *nptr - '0';
return (str_int(++nptr, sum));
}
int ft_atoi(const char *nptr)
{
int positive;
positive = 1;
while ((9 <= *nptr && *nptr <= 13) || *nptr == ' ')
nptr++;
if (*nptr == '-')
{
positive = -1;
nptr++;
}
else if (*nptr == '+')
{
nptr++;
}
return (positive * str_int(nptr, 0));
}
/*
int main(void)
{
printf("%d\n", ft_atoi(" +42"));
printf("%d\n", atoi(" +42"));
printf("%d\n", ft_atoi(" -42"));
printf("%d\n", atoi(" -42"));
printf("%d\n", ft_atoi(" +2147483647"));
printf("%d\n", atoi(" +2147483647"));
printf("%d\n", ft_atoi(" -2147483648"));
printf("%d\n", atoi(" -2147483648"));
printf("%d\n", ft_atoi(" -21 47483648"));
printf("%d\n", atoi(" -21 47483648"));
}
*/