mirror of
https://github.com/DavidGailleton/42-Push_Swap.git
synced 2026-01-27 08:41:58 +00:00
43 lines
1.3 KiB
C
43 lines
1.3 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_atoi.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: mteriier <mteriier@student.42lyon.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2025/12/08 18:28:17 by mteriier #+# #+# */
|
|
/* Updated: 2025/12/08 19:30:48 by mteriier ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include <stdlib.h>
|
|
|
|
static int calcul_sign(char c)
|
|
{
|
|
if (c == '-')
|
|
return (-1);
|
|
return (1);
|
|
}
|
|
|
|
int ft_atoi(const char *nptr)
|
|
{
|
|
size_t i;
|
|
int sign;
|
|
int tmp;
|
|
|
|
i = 0;
|
|
sign = 1;
|
|
tmp = 0;
|
|
while ((nptr[i] >= 9 && nptr[i] <= 13) || nptr[i] == ' ')
|
|
i++;
|
|
sign = calcul_sign(nptr[i]);
|
|
if (nptr[i] == '-' || nptr[i] == '+')
|
|
i++;
|
|
while (nptr[i] >= '0' && nptr[i] <= '9')
|
|
{
|
|
tmp = tmp * 10 + nptr[i] - '0';
|
|
i++;
|
|
}
|
|
return (tmp * sign);
|
|
}
|