1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
42-1st-piscine/ended/C04/ex04/ft_putnbr_base.c

63 lines
1.6 KiB
C
Raw Permalink Normal View History

2023-08-06 20:12:38 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}
}