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.

41 lines
1.4 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/18 17:06:25 by narnaud #+# #+# */
3 years ago
/* Updated: 2022/03/25 14:14:45 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
#include "../libft.h"
int ft_atoi(const char *str)
{
int sign;
3 years ago
double nb;
3 years ago
nb = 0;
sign = 1;
3 years ago
if (!ft_strncmp(str, "-2147483648", 11))
return (-2147483648);
3 years ago
while (*str == ' ' || (*str >= '\t' && *str <= '\r'))
str++;
if (*str == '-' && ++str)
sign = -1;
else if (*str == '+')
str++;
while (*str >= '0' && *str <= '9')
{
3 years ago
nb = nb * 10 + (*str - '0') % 10;
if (nb > 2147483647 && sign > 0)
3 years ago
return (-1);
3 years ago
else if (nb > 2147486648 && sign < 0)
3 years ago
return (0);
str++;
}
return ((int)(sign * nb));
}