Projet de l'école 42 : Libft
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

38 lines
1.3 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 17:06:25 by narnaud #+# #+# */
/* Updated: 2022/03/24 09:22:48 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_atoi(const char *str)
{
int sign;
int nb;
nb = 0;
sign = 1;
while (*str == ' ' || (*str >= '\t' && *str <= '\r'))
str++;
if (*str == '-' && ++str)
sign = -1;
else if (*str == '+')
str++;
while (*str >= '0' && *str <= '9')
{
if (nb < 0 && sign > 0)
return (-1);
else if (nb < 0 && sign < 0)
return (0);
nb = nb * 10 + (*str - '0') % 10;
str++;
}
return ((int)(sign * nb));
}