woody-woodpacker/ft_printf/libft/ft_atoi.c

36 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pbonilla <eirodeis.lepnj@gmail.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/11/11 16:00:14 by pbonilla #+# #+# */
/* Updated: 2020/11/18 14:03:00 by pbonilla ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
{
int nbr;
int sgn;
sgn = 1;
nbr = 0;
while (*nptr && (*nptr == ' ' || *nptr == '\n' || *nptr == '\t' ||
*nptr == '\v' || *nptr == '\f' || *nptr == '\r'))
++nptr;
if (*nptr == '-')
sgn *= -1;
if (*nptr == '-' || *nptr == '+')
++nptr;
while (*nptr && *nptr >= '0' && *nptr <= '9')
{
nbr = nbr * 10 + (*nptr - 48);
++nptr;
}
return (nbr * sgn);
}