-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
50 lines (45 loc) · 1.32 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ewilliam <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/29 14:28:56 by ewilliam #+# #+# */
/* Updated: 2016/12/08 15:00:00 by ewilliam ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_sign(char c)
{
if (c == '-' || c == '+')
return (1);
else
return (0);
}
static int char_to_int(char c)
{
return (c - '0');
}
int ft_atoi(const char *str)
{
long val;
int sign;
val = 0;
sign = 1;
while (ft_isspace(*str))
++str;
if (is_sign(*str))
{
if (*str == '-')
sign = -1;
++str;
}
while (ft_isdigit(*str))
{
val *= 10;
val += char_to_int(*str);
++str;
}
return ((int)val * sign);
}