/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* ft_putnbr_base.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: whaffman +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/11 17:49:33 by whaffman #+# #+# */ /* Updated: 2024/06/12 18:50:38 by whaffman ### ########.fr */ /* */ /* ************************************************************************** */ #include int ft_strlen(char *str) { int n; n = 0; while (str[n]) n++; return (n); } int ft_isrepeating(char *str) { int i; while (*str) { i = 1; while (str[i]) { if (str[0] == str[i]) return (1); i++; } str++; } return (0); } int ft_contains_illegal_char(char *str) { while (*str) { if (*str == '-' || *str == '+') return (1); str++; } return (0); } void ft_putnbr_base(int nbr, char *base) { int b; b = ft_strlen(base); if (ft_isrepeating(base) || b < 2 || ft_contains_illegal_char(base)) return ; if (nbr >= 0 && nbr < b) { write(1, &base[nbr], 1); } else if (nbr < 0) { write(1, "-", 1); if (nbr > -b) ft_putnbr_base(-nbr, base); else { ft_putnbr_base(nbr / (-b), base); ft_putnbr_base(-(nbr % b), base); } } else if (nbr >= b) { ft_putnbr_base(nbr / b, base); ft_putnbr_base(nbr % b, base); } } #ifdef DEBUG int main(void) { ft_putnbr_base(123, "012345678"); } #endif