-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
73 lines (66 loc) · 1.83 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emdi-mar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/08 18:20:58 by emdi-mar #+# #+# */
/* Updated: 2024/11/08 18:21:20 by emdi-mar ### ########.fr */
/* */
/* ************************************************************************** */
/*
** LIBRARY: libft
** SYNOPSIS: convert ASCII string to integer
**
** DESCRIPTION:
** The atoi() function converts the initial portion of the string pointed
** to by str to int representation.
**
** The function has an undefined behavior if the representation of the string
** overflows the type int
*/
static const char *jump_space(const char *str)
{
while (*str == 32 || (*str > 8 && *str < 14))
str++;
return (str);
}
static const char *jump_sign(const char *str, int *sign)
{
if (*str == 43 || *str == 45)
{
if (*str == 45)
*sign = *sign * -1;
str++;
}
return (str);
}
static int calculate(const char *str)
{
int nbr;
int k;
nbr = 0;
k = 0;
while (*str > 47 && *str < 58)
{
nbr = nbr * 10;
k = *str;
nbr = nbr + (k - 48);
str++;
}
return (nbr);
}
int ft_atoi(const char *str)
{
int sign;
int nbr;
int *s;
sign = 1;
s = &sign;
str = jump_space(str);
str = jump_sign(str, s);
nbr = calculate(str);
nbr = nbr * sign;
return (nbr);
}