blob: 0d4247b2bc04bb4353bd74016ceaee92a55c481c (
plain)
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: cacharle <charles.cabergs@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/07/06 07:31:38 by cacharle #+# #+# */
/* Updated: 2019/07/06 08:36:47 by cacharle ### ########.fr */
/* */
/* ************************************************************************** */
int pow10(int exponent)
{
int accumulator;
accumulator = 1;
while (exponent > 0)
{
accumulator *= 10;
exponent--;
}
return (accumulator);
}
int ft_atoi(char *str)
{
int is_negative;
int nb;
int i;
while (*str == ' ' || *str == '\t' || *str == '\n'
|| *str == '\v' || *str == '\f' || *str == '\r')
str++;
while (*str == '-' || *str == '+')
{
if (*str == '-')
is_negative = !is_negative;
str++;
}
i = 0;
while (str[i] >= '0' && str[i] <= '9')
{
nb += pow10(i) * (str[i] - '0');
i++;
}
if (is_negative)
nb = -nb;
return (nb);
}
|