-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
58 lines (52 loc) · 1.66 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: alexmitcul <alexmitcul@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/17 16:28:41 by alexmitcul #+# #+# */
/* Updated: 2022/11/06 22:01:21 by alexmitcul ### ########.fr */
/* */
/* ************************************************************************** */
/**
* The atoi() function converts the initial portion of the string
* pointed to by str to int representation.
**/
#include "libft.h"
static int check_overflow(unsigned long n)
{
if ((n * 10) / 10 != n)
return (1);
return (0);
}
static int get_overflow_number(int sign)
{
if (sign == 1)
return (-1);
return (0);
}
int ft_atoi(const char *str)
{
int sign;
long long res;
sign = 1;
res = 0;
while (*str == ' ' || *str == '\f' || *str == '\n' || *str == '\r' || \
*str == '\t' || *str == '\v')
str++;
if (*str == '+' || *str == '-')
{
if (*str == '-')
sign = sign * (-1);
str++;
}
while (*str != '\0' && (*str >= '0' && *str <= '9'))
{
if (check_overflow(res))
return (get_overflow_number(sign));
res = res * 10 + (*str - '0');
str++;
}
return (res * sign);
}