42_C_PISCINE/C/c04v1/ex05/ft_atoi_base.c

74 lines
1.7 KiB
C
Raw Permalink Normal View History

2023-05-17 12:45:25 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/19 14:48:20 by cchauvet #+# #+# */
/* Updated: 2022/07/25 10:01:52 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
int ft_baser(char *str)
{
int i;
int j;
int count;
i = -1;
while (str[++i] != 0)
{
count = 0;
j = 0;
while (str[j] != 0)
if (str[i] == str[j++])
count++;
if (count > 1 || str[i] == '+' || str[i] == '-')
return (0);
}
if (i == 1)
return (0);
return (i);
}
int ft_find(char *str, char to_find)
{
int i;
i = 0;
while (str[i] != 0)
{
if (str[i] == to_find)
return (i);
i++;
}
return (-1);
}
int ft_atoi_base(char *str, char *base)
{
int base_size;
int sign;
int nbr;
int i;
nbr = 0;
sign = 1;
base_size = ft_baser(base);
i = 0;
if (base == 0)
return (0);
while (str[i] != ' ')
i++;
while (str[i] == '+' || str[i] == '-')
if (str[i++] == '-')
sign = sign * -1;
while (ft_find(base, str[i]) != -1)
{
nbr = nbr * base_size + ft_find(base, str[i]);
str++;
}
return (nbr * sign);
}