-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathft_atoi.c
More file actions
63 lines (58 loc) · 1.72 KB
/
Copy pathft_atoi.c
File metadata and controls
63 lines (58 loc) · 1.72 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cado-car <cado-car@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/30 20:20:48 by cado-car #+# #+# */
/* Updated: 2021/08/03 18:38:51 by cado-car ### ########lyon.fr */
/* */
/* ************************************************************************** */
/*
* LIBRARY
* #include <stdlib.h>
* DESCRIPTION
* The atoi() function converts the initial portion of the string pointed to by
* str to int representation.
* PARAMETERS
* #1. The string to convert to integer.
* RETURN VALUES
* The atoi() function returns its converted int representation.
*/
#include "libft.h"
int ft_checkerr(int sign);
int ft_atoi(const char *str)
{
size_t i;
int sign;
long total;
long prev;
i = 0;
sign = 1;
total = 0;
while ((str[i] >= 9 && str[i] <= 13) || str[i] == 32)
i++;
if (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
sign *= (-1);
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
prev = total;
total = total * 10 + (str[i] - '0');
if (total < prev)
return (ft_checkerr(sign));
i++;
}
return (total * sign);
}
int ft_checkerr(int sign)
{
if (sign > 0)
return (-1);
else
return (0);
}