first commit
This commit is contained in:
62
ended/C04/ex04/ft_putnbr_base.c
Normal file
62
ended/C04/ex04/ft_putnbr_base.c
Normal file
@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_putnbr_base.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/26 13:30:23 by ajoly #+# #+# */
|
||||
/* Updated: 2022/07/26 13:30:27 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
#include <unistd.h>
|
||||
|
||||
int check_base(char *base)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
if (base[0] == '\0' && base[1] == '\0')
|
||||
{
|
||||
return (0);
|
||||
}
|
||||
while (base[i])
|
||||
{
|
||||
if (base[i] < 26 || base[i] > 126)
|
||||
return (0);
|
||||
if (base[i] == '+' || base[i] == '-')
|
||||
return (0);
|
||||
if (base[i] == base[i + 1])
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void ft_putnbr_base(int nbr, char *base)
|
||||
{
|
||||
int base_count;
|
||||
int i;
|
||||
int final[255];
|
||||
|
||||
i = 0;
|
||||
base_count = 0;
|
||||
if (check_base(base))
|
||||
{
|
||||
if (nbr < 0)
|
||||
{
|
||||
nbr = -nbr;
|
||||
write(1, "-", 1);
|
||||
}
|
||||
while (base[base_count])
|
||||
base_count++;
|
||||
while (nbr)
|
||||
{
|
||||
final[i] = nbr % base_count;
|
||||
nbr = nbr / base_count;
|
||||
i++;
|
||||
}
|
||||
while (--i >= 0)
|
||||
write(1, &base[final[i]], 1);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user