-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathft_atoi.c
53 lines (48 loc) · 1.71 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
49
50
51
52
53
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fcil <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/21 18:57:53 by fcil #+# #+# */
/* Updated: 2022/02/04 10:30:27 by fcil ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include "limits.h"
static int ft_isspace(char chr)
{
if ((chr >= 9 && chr <= 13) || chr == ' ')
return (1);
return (0);
}
int ft_atoi(const char *nptr)
{
unsigned long ret_val;
int index;
int polarity;
polarity = 1;
index = 0;
if (*nptr == '\0')
return (0);
while (ft_isspace(nptr[index]))
index++;
if (nptr[index] == '-' || nptr[index] == '+')
{
if (nptr[index] == '-')
polarity *= -1;
index++;
}
ret_val = 0;
while (nptr[index] >= '0' && nptr[index] <= '9')
ret_val = (ret_val * 10) + (nptr[index++] - '0');
if (ret_val > LONG_MAX && polarity == -1)
return (0);
if (ret_val > LONG_MAX && polarity == 1)
return (-1);
return (ret_val * polarity);
}
//33. satirda bosluklari yok ediyoruz.
//35 de - mi + mi kontrol ediyoruz.
// 42 de basamak basamak rakamlari ekliyoruz. ve return ediyoruz.