-
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.45 KB
/
ft_atoi.c
File metadata and controls
41 lines (38 loc) · 1.45 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: gaducurt <gaducurt@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/18 12:37:28 by gaducurt #+# #+# */
/* Updated: 2026/01/14 16:18:15 by gaducurt ### ########.fr */
/* */
/* ************************************************************************** */
#include <limits.h>
int ft_atoi(const char *nptr)
{
int count;
int inv;
count = 0;
inv = 1;
while (*nptr == ' ' || ((*nptr >= 9 && *nptr <= 13) && *nptr != 0))
nptr++;
if (*nptr == '-' || *nptr == '+')
{
if (*nptr == '-')
inv = -inv;
nptr++;
}
while ((*nptr >= '0' && *nptr <= '9') && *nptr != '\0')
{
count = (count * 10) + (*nptr - '0');
if ((unsigned long long) count > (unsigned long long)INT_MAX + 1
&& inv == -1)
return (INT_MIN);
else if ((unsigned long long) count > INT_MAX && inv == 1)
return (INT_MAX);
nptr++;
}
return (count * inv);
}