-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
42 lines (39 loc) · 1.5 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: ohachim <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/05 22:08:07 by ohachim #+# #+# */
/* Updated: 2018/10/17 18:14:15 by ohachim ### ########.fr */
/* */
/* ************************************************************************** */
#include "stdio.h"
#include "libft.h"
int ft_atoi(const char *str)
{
long long int toi;
int cn;
char *dupstr;
cn = 0;
toi = 0;
dupstr = (char*)str;
if ((*str == ' ' || (*str >= 9 && *str <= 13)) &&
((*(str - 1) > '9' && *(str - 1) < '0') || str == dupstr))
toi = ft_atoi(++str);
else if (*str == '+' && *(str + 1) >= '0' && *(str + 1) <= '9')
toi = ft_atoi(++str);
else if (*str == '-' && *(str + 1) >= '0' && *(str + 1) <= '9')
{
toi = ft_atoi(++str);
return (toi * -1);
}
else
while (str[cn] >= '0' && str[cn] <= '9' && str[cn] != '\0')
{
toi = toi * 10 + (str[cn] - '0');
cn++;
}
return ((int)toi);
}