-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
46 lines (43 loc) · 1.4 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: thuy-ngu <thuy-ngu@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/02 21:22:25 by thuy-ngu #+# #+# */
/* Updated: 2023/10/21 00:18:41 by thuy-ngu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *str)
{
int i;
int value;
int sign;
i = 0;
value = 0;
sign = 1;
if (str[i] != '\0')
{
while ((str[i] >= '\t' && str[i] <= '\r') || str[i] == ' ')
i++;
if (str[i] == '-' || str[i] == '+')
{
if (str[i] == '-')
sign = sign * -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
value = value * 10 + (str[i] - 48);
i++;
}
}
return (value * sign);
}
/*int main(void)
{
printf("%d", ft_atoi(" +1234ab567"));
printf("%d", ft_atoi(" -1234ab567"));
}*/