42_Philosopher/utils/ft_atoi.c

40 lines
1.2 KiB
C
Raw Normal View History

2023-04-13 09:00:39 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/27 16:14:34 by cchauvet #+# #+# */
2023-04-27 08:05:14 -04:00
/* Updated: 2023/04/27 11:27:44 by cchauvet ### ########.fr */
2023-04-13 09:00:39 -04:00
/* */
/* ************************************************************************** */
#include "utils.h"
#include <stddef.h>
int ft_atoi(const char *str)
{
int out;
char sign;
size_t i;
sign = 1;
i = 0;
2023-04-27 08:05:14 -04:00
while (str[i] == '-' || str[i] == '+')
2023-04-13 09:00:39 -04:00
{
if (str[i] == '-')
sign = -1;
if (str[i] == '+')
sign = 1;
i++;
}
out = 0;
while (str[i] >= '0' && str[i] <= '9')
{
out = out * 10 + str[i] - 48;
i++;
}
return (out * sign);
}