-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
executable file
·77 lines (71 loc) · 1.88 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cyildiri <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/09/25 16:18:31 by cyildiri #+# #+# */
/* Updated: 2016/09/26 16:50:23 by cyildiri ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static void ft_inti_vars(int *i, int *num, int *multiplier)
{
*i = 0;
*num = 0;
*multiplier = 1;
}
static int ft_junk_filter(char **str, int *end_index)
{
int sign;
int sign_found;
sign = 1;
sign_found = 0;
while (!ft_isdigit((int)**str))
{
if (sign_found)
return (0);
if ((**str == ' ' || **str == '\t' || **str == '\n'
|| **str == '\v' || **str == '\f' || **str == '\r'))
(*str)++;
else if (**str == '-' || **str == '+')
{
if (**str == '-')
sign = -1;
sign_found = 1;
(*str)++;
}
else
return (0);
}
while ((*str)[*end_index] != '\0')
(*end_index)++;
return (sign);
}
int ft_atoi(const char *str)
{
int num;
int i;
int multiplier;
int sign;
char *ptr;
ft_inti_vars(&i, &num, &multiplier);
ptr = (char *)(str);
sign = ft_junk_filter(&ptr, &i);
while (i >= 0)
{
if (ft_isdigit((int)ptr[i]))
{
num += multiplier * (ptr[i] - '0');
multiplier *= 10;
}
else
{
num = 0;
multiplier = 1;
}
i--;
}
return (num * sign);
}