42_C_PISCINE/C/c04v2/ex03/ft_atoi.c

66 lines
1.5 KiB
C
Raw Permalink Normal View History

2023-05-17 12:45:25 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 10:08:23 by cchauvet #+# #+# */
/* Updated: 2022/07/25 18:42:46 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
int ft_whitespaces(char *str)
{
char *space;
int j;
int i;
space = "\n\r\v\t\n ";
i = 0;
j = 0;
while (space[i] != 0)
{
while (str[j] == space[i])
{
if (str[j] == 0)
return (j);
i = 0;
j++;
}
i++;
}
return (j);
}
int ft_atoi(char *str)
{
int i;
int sign;
int nbr;
sign = 1;
nbr = 0;
i = ft_whitespaces(str);
while (str[i] == '+' || str[i] == '-')
{
if (str[i] == '-')
sign = sign * -1;
i++;
}
while (str[i] >= '0' && str[i] <= '9')
{
nbr = nbr * 10 + str[i] - '0';
i++;
}
return (nbr * sign);
}
#include <stdio.h>
int main ()
{
printf("%d", ft_atoi(" +--+-++--00000132563hhi2167"));
}