-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
52 lines (48 loc) · 1.52 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mmalie <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/04 15:33:29 by mmalie #+# #+# */
/* Updated: 2024/11/14 09:27:24 by mmalie ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
* Implementation of atoi() from <stdlib.h>: Converts a string to an integer,
* stopping at the first non-numeric character.
*/
int ft_atoi(const char *nptr);
static int ft_isspace(int c);
int ft_atoi(const char *nptr)
{
int result;
int sign;
result = 0;
sign = 1;
while (ft_isspace(*nptr))
{
nptr++;
}
if (*nptr == '+' || *nptr == '-')
{
if (*nptr == '-')
{
sign = -1;
}
nptr++;
}
while (ft_isdigit(*nptr))
{
result = (result * 10) + (*nptr - '0');
nptr++;
}
return (result * sign);
}
static int ft_isspace(int c)
{
return (c == 32 || c == '\f' || c == '\n'
|| c == '\r' || c == '\t' || c == '\v');
}