-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
executable file
·42 lines (38 loc) · 1.45 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: luperez <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/05 19:37:31 by luperez #+# #+# */
/* Updated: 2014/11/15 17:07:41 by luperez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int is_c_white(char grey)
{
if (grey != '-' && grey != '+' && (grey == ' ' || grey == '\t' ||
grey == '\r' || grey == '\n' || grey == '\v' || grey == '\f'))
return (1);
return (0);
}
int ft_atoi(const char *s)
{
int len;
int nbr;
int i;
int mult;
i = 0;
nbr = 0;
len = ft_strlen(s);
while (is_c_white(s[i]) || s[i] == '0')
++i;
mult = ((s[i] == '+' || s[i] == '-') && ++i && s[i - 1] == '-') ? -1 : 1;
while (i < len)
if (s[i] < 48 || s[i] > 57)
return (nbr * mult);
else
nbr = (nbr * 10) + (s[i++] - '0');
return (nbr * mult);
}