-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_atoi.c
More file actions
40 lines (37 loc) · 1.36 KB
/
ft_atoi.c
File metadata and controls
40 lines (37 loc) · 1.36 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ppanchen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/28 12:45:29 by ppanchen #+# #+# */
/* Updated: 2016/11/29 18:42:21 by ppanchen ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
{
int i;
int is_negative;
int res;
i = 0;
res = 0;
is_negative = 0;
while (nptr[i] == '\n' || nptr[i] == '\t' || nptr[i] == '\v' ||
nptr[i] == '\r' || nptr[i] == '\f' || nptr[i] == ' ')
i++;
if (nptr[i] == '-')
is_negative = 1;
if (nptr[i] == '-' || nptr[i] == '+')
i++;
while (nptr[i] >= 48 && nptr[i] <= 57 && nptr[i])
{
res *= 10;
res += nptr[i] - '0';
i++;
}
if (is_negative == 1)
return (-res);
return (res);
}