42_libft/ft_atoi.c

40 lines
1.3 KiB
C
Raw Normal View History

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