ft_atoi: correctly handle +/- at start
This commit is contained in:
parent
9b93b1c909
commit
afcddc5e14
1 changed files with 10 additions and 2 deletions
12
ft_atoi.c
12
ft_atoi.c
|
|
@ -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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue