-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
45 lines (41 loc) · 1.35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ahmaymou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/08 14:32:05 by ahmaymou #+# #+# */
/* Updated: 2022/10/26 17:10:48 by ahmaymou ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int is_whitespace(char c)
{
if (c == '\t' || c == '\n' || c == '\v' || c == '\f'
|| c == '\r' || c == ' ')
return (1);
return (0);
}
int ft_atoi(const char *str)
{
int result;
int sign;
result = 0;
sign = 1;
while (is_whitespace(*str))
str++;
if (*str && (*str == '-' || *str == '+'))
{
if (*str == '-')
sign *= (-1);
str++;
}
while (*str && ft_isdigit((*str)))
{
result *= 10;
result += *str - '0';
str++;
}
return (result * (sign));
}