ft_atoi: correctly handle +/- at start

This commit is contained in:
Khaïs COLIN 2024-10-19 18:11:30 +02:00
parent 9b93b1c909
commit afcddc5e14

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/16 16:06:11 by kcolin #+# #+# */ /* Created: 2024/10/16 16:06:11 by kcolin #+# #+# */
/* Updated: 2024/10/18 17:58:42 by kcolin ### ########.fr */ /* Updated: 2024/10/19 17:59:34 by kcolin ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -29,18 +29,26 @@ int ft_atoi(const char *nptr)
{ {
int result; int result;
int i; int i;
int sign;
result = 0; result = 0;
i = 0; i = 0;
sign = 1;
while (ft_isspace(nptr[i])) while (ft_isspace(nptr[i]))
i++; i++;
if (nptr[i] == '+' || nptr[i] == '-')
{
if (nptr[i] == '-')
sign = -1;
i++;
}
while (ft_isdigit(nptr[i])) while (ft_isdigit(nptr[i]))
{ {
result *= 10; result *= 10;
result += nptr[i] - '0'; result += nptr[i] - '0';
i++; i++;
} }
return (result); return (result * sign);
} }
/* /*