-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathft_atoi.c
43 lines (40 loc) · 1.31 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: smbaabu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/02/21 22:27:53 by smbaabu #+# #+# */
/* Updated: 2019/02/23 20:26:57 by smbaabu ### ########.fr */
/* */
/* ************************************************************************** */
static int is_space(char c)
{
return (c == ' ' || c == '\t' || c == '\n'
|| c == '\v' || c == '\f' || c == '\r');
}
int ft_atoi(const char *str)
{
int i;
int neg;
int sum;
sum = 0;
neg = 0;
i = 0;
while (is_space(str[i]))
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
neg = 1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
sum *= 10;
sum += str[i] - '0';
i++;
}
return (neg ? -(sum) : sum);
}