-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
47 lines (43 loc) · 1.37 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nlyamani <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/01 17:47:18 by nlyamani #+# #+# */
/* Updated: 2024/10/01 17:47:23 by nlyamani ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_isspace(int c)
{
if (c == '\t' || c == '\n' || c == '\v')
return (-1);
if (c == '\f' || c == '\r' || c == ' ')
return (-1);
return (0);
}
int ft_atoi(const char *str)
{
int sign;
int result;
sign = 1;
result = 0;
while (ft_isspace(*str))
str++;
if (*str == '+' && *(str + 1) != '-')
str++;
if (*str == '-')
{
sign = -1;
str++;
}
while (*str && ft_isdigit(*str))
{
result *= 10;
result += *str++ - '0';
}
result *= sign;
return (result);
}