-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_atoi.c
48 lines (43 loc) · 1.41 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
48
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juhtoo-h <juhtoo-h@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/27 08:27:01 by juhtoo-h #+# #+# */
/* Updated: 2024/08/30 09:38:21 by juhtoo-h ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
{
int i;
int sign;
int res;
i = 0;
sign = 1;
res = 0;
while (nptr[i] == ' ' || (nptr[i] >= 7 && nptr[i] <= 13))
i++;
if (nptr[i] == '-' || nptr[i] == '+')
{
if (nptr[i] == '-')
sign *= -1;
i++;
}
while (nptr[i] >= '0' && nptr[i] <= '9')
{
res = res * 10 + (nptr[i] - '0');
i++;
}
return (sign * res);
}
// #include "stdio.h"
// #include "string.h"
// int main(void)
// {
// char a[20] = "214748364asdfc";
// printf("%d\n", atoi(a));
// printf("%d\n", ft_atoi(a));
// }