-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
80 lines (71 loc) · 1.91 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kyalexan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/22 10:26:01 by kyalexan #+# #+# */
/* Updated: 2021/12/15 12:58:25 by kyalexan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(char c)
{
return (c == ' ' || c == '\n' \
|| c == '\t' || c == '\v' \
|| c == '\f' || c == '\r');
}
static void handle_spaces(const char *nptr, int *i)
{
while (ft_isspace(nptr[*i]))
(*i)++;
}
static int handle_sign(char c, int *i)
{
int sign;
sign = 1;
if (c == '-')
{
sign = -1;
(*i)++;
}
else if (c == '+')
(*i)++;
return (sign);
}
static int ft_strcmp(const char *s1, const char *s2)
{
int i;
i = 0;
while (s1[i] || s2[i])
{
if (s1[i] != s2[i])
return ((unsigned char)s1[i] - (unsigned char)s2[i]);
i++;
}
return (0);
}
int ft_atoi(const char *nptr)
{
int number;
int sign;
int i;
i = 0;
handle_spaces(nptr, &i);
sign = handle_sign(nptr[i], &i);
number = 0;
if (!ft_strcmp(nptr + i, "0"))
return (0);
else if (!ft_strcmp(nptr + i, "2147483647"))
return (2147483647);
else if (!ft_strcmp(nptr + i, "2147483648") && sign == -1)
return (-2147483648);
while (ft_isdigit(nptr[i]))
{
number *= 10;
number += nptr[i] - '0';
i++;
}
return (number * sign);
}