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.
39 lines
1.3 KiB
39 lines
1.3 KiB
3 years ago
|
/* ************************************************************************** */
|
||
|
/* */
|
||
|
/* ::: :::::::: */
|
||
|
/* ft_atoi.c :+: :+: :+: */
|
||
|
/* +:+ +:+ +:+ */
|
||
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
||
|
/* +#+#+#+#+#+ +#+ */
|
||
|
/* Created: 2021/10/18 17:06:25 by narnaud #+# #+# */
|
||
|
/* Updated: 2021/11/15 10:01:15 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));
|
||
|
}
|