-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
42 lines (39 loc) · 1.31 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
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_atoi.c :+: :+: */
/* +:+ */
/* By: mgraaf <mgraaf@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2021/12/16 14:18:44 by mgraaf #+# #+# */
/* Updated: 2021/12/16 14:18:46 by mgraaf ######## odam.nl */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int ret;
int p_n;
int i;
i = 0;
ret = 0;
p_n = 1;
while (*str == ' ' || (*str > 8 && *str < 14))
str++;
if (*str == '-' || *str == '+')
{
if (*str == '-')
p_n *= -1;
str++;
}
while (ft_isdigit(str[i]) == 1)
{
ret = (ret * 10) + (str[i] - '0');
i++;
if (i == 10 && str[i] && p_n == -1)
return (0);
else if (i == 10 && str[i])
return (-1);
}
return (ret * p_n);
}