-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
53 lines (49 loc) · 1.58 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
53
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chrhuang <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/07 14:38:23 by chrhuang #+# #+# */
/* Updated: 2018/11/19 11:01:34 by chrhuang ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static const char *check_my_str(const char *str)
{
while (*str < '0' || *str > '9')
{
if (*str != '+' && *str != '-' && *str != '\0')
{
if (*str == '\n' || *str == '\f' || *str == '\r')
str = str + 1;
else if (*str == '\v' || *str == '\t' || *str == ' ')
str = str + 1;
else
return (NULL);
}
else
return (str);
}
return (str);
}
int ft_atoi(const char *str)
{
int nb;
int i;
int neg;
nb = 0;
i = 0;
if ((str = check_my_str(str)) == NULL)
return (0);
neg = str[0] == '-' ? 1 : 0;
if (neg == 1 || *str == '+')
str = str + 1;
while (*str <= '9' && *str >= '0')
{
nb = nb * 10 + *str - '0';
str = str + 1;
}
return (nb = (neg == 1 ? -nb : nb));
}