sync
This commit is contained in:
57
C_Piscine_C_04/git/ex05/ft_atoi_base.c
Normal file
57
C_Piscine_C_04/git/ex05/ft_atoi_base.c
Normal file
@@ -0,0 +1,57 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi_base.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: gbaconni <marvin@42lausanne.ch> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2021/08/16 13:53:46 by gbaconni #+# #+# */
|
||||
/* Updated: 2021/08/16 15:05:08 by gbaconni ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
static int ft_strlen(char *str)
|
||||
{
|
||||
int size;
|
||||
|
||||
size = 0;
|
||||
while (str[++size] != '\0')
|
||||
continue ;
|
||||
return (size);
|
||||
}
|
||||
|
||||
static int ft_nbase(char c, char *base)
|
||||
{
|
||||
int n;
|
||||
|
||||
n = 0;
|
||||
while (base[n] != '\0' && base[n] != c)
|
||||
n++;
|
||||
return (n);
|
||||
}
|
||||
|
||||
int ft_atoi_base(char *str, char *base)
|
||||
{
|
||||
int nb;
|
||||
int s;
|
||||
int i;
|
||||
int size;
|
||||
|
||||
size = ft_strlen(base);
|
||||
nb = 0;
|
||||
s = 1;
|
||||
i = 0;
|
||||
while (str[i] != '\0' && (str[i] == ' ' || (str[i] >= '\t' && str[i] <= '\r')))
|
||||
i++;
|
||||
while (str[i] != '\0')
|
||||
{
|
||||
if (str[i] == '+')
|
||||
s += 0;
|
||||
else if (str[i] == '-')
|
||||
s *= -1;
|
||||
else
|
||||
nb = nb * size + ft_nbase(str[i], base);
|
||||
i++;
|
||||
}
|
||||
return (nb * s);
|
||||
}
|
||||
Reference in New Issue
Block a user