-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
50 lines (47 loc) · 1.51 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: fraqioui <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/05 14:19:05 by fraqioui #+# #+# */
/* Updated: 2022/10/20 09:23:01 by fraqioui ### ########.fr */
/* */
/* ************************************************************************** */
#include"libft.h"
static char *ft_checksign(const char *s, int *c)
{
if (*s == '+')
s++;
else if (*s == '-')
{
*c = -1;
s++;
}
return ((char *)s);
}
int ft_atoi(const char *str)
{
int sign;
long long res;
long long prev;
char *s;
sign = 1;
res = 0;
while (*str != '\0' && (*str == ' ' || *str == '\n' || *str == '\t'
||*str == '\f' || *str == '\r' || *str == '\v'))
str++;
s = ft_checksign(str, &sign);
while (*s >= '0' && *s <= '9')
{
prev = res;
res = res * 10 + (*s - 48);
if (prev != res / 10 && sign == 1)
return (-1);
else if (prev != res / 10 && sign == -1)
return (0);
s++;
}
return (sign * res);
}