-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
52 lines (48 loc) · 1.56 KB
/
Copy pathft_atoi.c
File metadata and controls
52 lines (48 loc) · 1.56 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: imelnych <imelnych@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/21 09:48:45 by imelnych #+# #+# */
/* Updated: 2018/01/27 10:28:45 by imelnych ### ########.fr */
/* */
/* ************************************************************************** */
#include "printflib.h"
static int ft_isdelim(char c)
{
if (c == ' ' || c == '\n' || c == '\t' ||
c == '\v' || c == '\r' || c == '\f')
{
return (1);
}
return (0);
}
int ft_atoi(const char *str)
{
int i;
int is_negative;
long num;
int numlen;
i = 0;
num = 0;
numlen = 0;
while (ft_isdelim(str[i]))
i++;
is_negative = (str[i] == '-') ? 1 : 0;
if (str[i] == '-' || str[i] == '+')
i++;
if (str[i] < 48 || str[i] > 57)
return (0);
while (str[i] >= 48 && str[i] <= 57)
{
num = num * 10;
num = num + str[i++] - '0';
numlen++;
}
num = (is_negative) ? num * (-1) : num;
if (numlen > 19 || num > 9223372036854775807)
return ((is_negative) ? 0 : -1);
return ((int)num);
}