-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
70 lines (63 loc) · 1.85 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pgober <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/04 16:57:46 by pgober #+# #+# */
/* Updated: 2023/11/05 10:33:03 by pgober ### ########.fr */
/* */
/* ************************************************************************** */
#include "push_swap.h"
static int is_space(char str)
{
if (str == 32 || (9 <= str && str <= 13))
return (1);
return (0);
}
int is_num(char str)
{
if ('0' <= str && str <= '9')
return (1);
return (0);
}
static int is_pm(char str)
{
if (str == '+' || str == '-')
return (1);
return (0);
}
static int is_valid(char str)
{
if (is_space(str) == 1 || is_num(str) == 1 || is_pm(str) == 1)
return (1);
return (0);
}
int ft_atoi(const char *nptr, int *error)
{
int res;
int sign;
sign = 1;
res = 0;
while (*nptr != '\0' && is_valid(*nptr) == 1)
{
while (is_space(*nptr) == 1 && is_space(*(nptr + 1)) == 1)
nptr++;
if (is_pm(*nptr) == 1 && is_num(*(nptr + 1)) == 0)
return (0);
else if (*nptr == '-')
sign = sign * (-1);
while (is_num(*nptr) == 1)
{
if (!ft_atoi_checker(res, *nptr, sign))
return (*error = 1, 0);
res = res * 10 + *nptr - '0';
if (is_num(*(nptr + 1)) == 0)
return (sign * res);
nptr++;
}
nptr++;
}
return (sign * res);
}