42_minishell/libftx/libft/ft_atoi.c

40 lines
1.3 KiB
C
Raw Permalink Normal View History

2023-01-31 08:40:15 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
/* Updated: 2022/09/28 18:15:04 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_atoi(const char *nptr)
{
char *str;
int out;
int sign;
str = (char *) nptr;
out = 0;
while (*str == ' ' || *str == '\f' || *str == '\v' || *str == '\t'
|| *str == '\r' || *str == '\n')
str++;
sign = 1;
if (*str == '-' || *str == '+')
{
if (*str == '-')
sign = -1;
str++;
}
while (ft_isdigit(*str))
{
out = 10 * out + *str - 48;
str++;
}
return (out * sign);
}