-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
executable file
·46 lines (42 loc) · 1.44 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: mwilliam <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 17:29:37 by mwilliam #+# #+# */
/* Updated: 2016/12/02 20:58:41 by mwilliam ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** Converts str to int representation
*/
int ft_atoi(const char *str)
{
int i;
int sign;
int result;
i = 0;
sign = 1;
result = 0;
while (ft_isspace(str[i]) && str[i] != '\0')
i++;
if (str[i] == '-' && (str[i + 1] - 48) >= 0 && (str[i + 1] - 48) <= 9)
{
sign = -1;
i++;
}
if (str[i] == '+' && (str[i + 1] - 48) >= 0 && (str[i + 1] - 48) <= 9)
i++;
while (str[i] != '\0')
{
if ((str[i] - 48) >= 0 && (str[i] - 48) <= 9)
result = result * 10 + (str[i] - 48);
else
return (sign * result);
i++;
}
return (sign * result);
}