-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_atoi.c
56 lines (50 loc) · 1.69 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
54
55
56
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adiaz-lo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/12 11:18:21 by adiaz-lo #+# #+# */
/* Updated: 2020/01/14 09:42:38 by adiaz-lo ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Function that converts a received as a parameter string to a integer
** For further information, please check the Standard C Library function
** 'atoi(const char *str)'
*/
#include "libft.h"
/*
** This auxiliar function checks if the character received by parameter is a
** space or not.
** For further information, please check the Standard C Library function
** 'isspace(int c)'.
*/
static int ft_isspace(int c)
{
if (c == '\v' || c == '\n' || c == '\t' ||
c == '\r' || c == '\f' || c == ' ')
return (1);
return (0);
}
int ft_atoi(const char *str)
{
int sign;
int result;
sign = 1;
result = 0;
while (ft_isspace(*str))
str++;
if (*str == '+' || *str == '-')
{
if (*str == '-')
sign *= -1;
str++;
}
while (ft_isdigit(*str))
{
result = result * 10 + (*str++ - 48);
}
return (result * sign);
}