first commit
This commit is contained in:
70
finish/C04/ex04/ft_putnbr_base.c
Normal file
70
finish/C04/ex04/ft_putnbr_base.c
Normal file
@ -0,0 +1,70 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_base.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/19 14:25:29 by adjoly #+# #+# */
|
||||
/* Updated: 2023/07/25 09:20:02 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_strlen(char *str)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (str[i])
|
||||
i++;
|
||||
return (i);
|
||||
}
|
||||
|
||||
int ft_checkbase(char *base)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
|
||||
i = 0;
|
||||
if (ft_strlen(base) <= 1)
|
||||
return (1);
|
||||
while (base[i])
|
||||
{
|
||||
if (base[i] == '-' || base[i] == '+')
|
||||
return (1);
|
||||
j = i + 1;
|
||||
while (base[j])
|
||||
{
|
||||
if (base[i] == base[j])
|
||||
return (1);
|
||||
j++;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void ft_putnbr_base(int nbr, char *base)
|
||||
{
|
||||
unsigned int len;
|
||||
unsigned int y;
|
||||
|
||||
if (ft_checkbase(base) == 1)
|
||||
return ;
|
||||
len = ft_strlen(base);
|
||||
if (nbr < 0)
|
||||
{
|
||||
write(1, "-", 1);
|
||||
y = -nbr;
|
||||
}
|
||||
else
|
||||
y = nbr;
|
||||
if (y < len)
|
||||
write(1, &(char){base[y]}, 1);
|
||||
else
|
||||
{
|
||||
ft_putnbr_base(y / len, base);
|
||||
write(1, &(char){base[y % len]}, 1);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user