-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
46 lines (43 loc) · 1.44 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_atoi.c :+: :+: */
/* +:+ */
/* By: cherrewi <[email protected]> +#+ */
/* +#+ */
/* Created: 2022/10/19 15:06:52 by cherrewi #+# #+# */
/* Updated: 2022/10/23 12:18:40 by cherrewi ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
Atoi = Array To Integer
Parses a string with digits to an int.
The first char can be a + or - sign.
Leading white spaces are skipped.
*/
int ft_atoi(const char *str)
{
long int num;
int sign;
num = 0;
sign = 1;
while (*str == ' ' || *str == '\f' || *str == '\n'
|| *str == '\r' || *str == '\t' || *str == '\v')
str++;
if (*str == '-')
sign = -1;
if (*str == '-' || *str == '+')
str++;
while (*str)
{
if (!ft_isdigit((int)(*str)))
{
break ;
}
num = num * 10;
num = num + ((*str) - 48);
str++;
}
return ((sign) * (num));
}