-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_atoi.c
54 lines (51 loc) · 1.72 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nholbroo <nholbroo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/16 13:35:52 by nholbroo #+# #+# */
/* Updated: 2025/01/30 13:48:14 by nholbroo ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
Which function:
Equivalent to the function "atoi" in stdlib.h.
Definition:
The atoi() function converts the initial portion of the string pointed
to by nptr to int.
Return values:
If successful -> The converted number.
If error -> Returns 0. Doesn't set errno, so not possible to distinguish
the number '0' from error.
@param nptr The number in ascii (as a string).
@param result The number as an int.
@param sign Indicates if the number is negative or positive.
*/
int ft_atoi(const char *nptr)
{
int i;
int result;
int sign;
i = 0;
result = 0;
sign = 1;
while ((nptr[i] >= 9 && nptr[i] <= 13)
|| (nptr[i] == ' '))
i++;
if (nptr[i] == '-')
{
sign *= -1;
i++;
}
else if (nptr[i] == '+')
i++;
while (nptr[i] >= '0' && nptr[i] <= '9')
{
result = result * 10 + (nptr[i] - '0');
i++;
}
return (result * sign);
}