-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
57 lines (52 loc) · 1.76 KB
/
ft_atoi.c
File metadata and controls
57 lines (52 loc) · 1.76 KB
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: kmoutaou <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/13 14:33:32 by kmoutaou #+# #+# */
/* Updated: 2021/11/19 14:04:03 by kmoutaou ### ########.fr */
/* */
/* ************************************************************************** */
/*
ft_atoi is a re-coded -atoi- function from <stdlib.h> that converts the
string pointed to by str to an integer.
*/
#include "libft.h"
static int ft_convert(const char *str, int i, int result, int sign)
{
while (ft_isdigit(str[i]))
{
if ((((unsigned long)result * 10) + (str[i] - '0'))
> (unsigned long)9223372036854775807 && sign > 0)
return (-1);
if ((((unsigned long)result * 10) + (str[i] - '0'))
> (unsigned long)9223372036854775807 + 1 && sign < 0)
return (0);
result = (result * 10) + (str[i] - '0');
i++;
}
return (result * sign);
}
int ft_atoi(const char *str)
{
int result;
int sign;
int i;
int repeat;
repeat = 0;
result = 0;
sign = 1;
i = 0;
while ((str[i] >= 9 && str[i] <= 13) || (str[i] == 32))
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign = -1;
i++;
}
result = ft_convert(str, i, result, sign);
return (result);
}