-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
49 lines (46 loc) · 1.58 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rruiz-sa <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/23 18:30:26 by rruiz-sa #+# #+# */
/* Updated: 2022/10/13 20:38:25 by rruiz-sa ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int result;
int i;
char *temp;
int neg;
i = 0;
neg = 1;
result = 0;
temp = (char *)str;
while (str[i] == ' ' || (temp[i] >= 9 && temp[i] <= 13))
i++;
if (str[i] == '-')
{
neg = -1;
i++;
}
else if (str[i] == '+')
i++;
while (ft_isdigit(temp[i]) == 1)
{
result = (result * 10) + (temp[i] - '0');
i++;
}
return (result * neg);
}
/*
1- The first Iteration, "erase" the blanks and special comands
from the cont(i) of the string(str)
2- Detect negative or positive(neg)
3- Iterate the rest of the string while the cont its equal to a number,
and saving it in a new variable * ten
4- Return the new value with the correct symbol (-+)
*/