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-2nd-piscine/finish/C04/ex04/ft_putnbr_base.c

71 lines
1.6 KiB
C
Raw Normal View History

2023-08-03 23:16:27 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* 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);
}
}