-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
41 lines (38 loc) · 1.34 KB
/
ft_atoi.c
File metadata and controls
41 lines (38 loc) · 1.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: togauthi <togauthi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/09 09:29:27 by togauthi #+# #+# */
/* Updated: 2024/10/20 14:09:03 by togauthi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
{
int p;
int i;
long res;
p = 1;
i = 0;
res = 0;
while ((nptr[i] >= '\t' && nptr[i] <= '\r') || nptr[i] == ' ')
i++;
if (nptr[i] == '+' || nptr[i] == '-')
if (nptr[i++] == '-')
p = -1;
while (nptr[i] >= '0' && nptr[i] <= '9')
{
if (res > (LLONG_MAX - (nptr[i] - '0')) / 10)
{
if (p == 1)
return (-1);
else
return (0);
}
res = res * 10 + (nptr[i++] - '0');
}
return ((int)(res * p));
}