-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
68 lines (60 loc) · 2.15 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
57
58
59
60
61
62
63
64
65
66
67
68
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sperez-p <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/20 12:48:08 by sperez-p #+# #+# */
/* Updated: 2021/09/06 17:12:28 by sperez-p ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <unistd.h>
/*
NAME
****
ft_atoi ---> array to integer
SYNOPSIS
***********
#include <stdlib.h>
int atoi(const char *str);
#include "libft.h"
int ft_atoi(const char *str);
DESCRIPTION
***********
The function ft_atoi() mimics the behavior of the atoi() function from
<stdlib.h> library. Both functions convert the string pointed to by
str to int. The behaviour is the same as "strtol(str, NULL, 10);" except
that it detect errors.
PARAMETERS
**********
str ---> The string representation of an integral number.
RETURN VALUE
************
This function returns the converted value as an int value. If there is
an error returns zero.
BUGS
****
errno is not set on error so there is no way to distinguish between 0 as
an error and as as the converted value. No check for overflow or underflow
are done. Only base-10 input can be converted.
*/
int ft_atoi(const char *str)
{
int result;
ssize_t sign;
result = 0;
sign = 1;
while ((*str >= 9 && *str <= 13) || *str == 32)
str++;
if (*str == 43 || *str == 45)
{
if (*str == 45)
sign = -1;
str++;
}
while (ft_isdigit(*str))
result = (*str++ - 48) + (result * 10);
return (result * sign);
}