42_C_PISCINE/Rush/Rush02/ex00/ft_tabnbr.c

48 lines
1.3 KiB
C
Raw Permalink Normal View History

2023-05-17 12:45:25 -04:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tabnbr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/31 20:57:07 by cchauvet #+# #+# */
/* Updated: 2022/07/31 20:57:09 by cchauvet ### ########.fr */
/* */
/* ************************************************************************** */
#include "rush02.h"
int ft_lenght(unsigned int nb)
{
int a;
a = 1;
while (nb / 10 != 0)
{
nb = nb / 10;
a = a * 10;
}
return (a);
}
char *ft_tabnbr(unsigned int nb)
{
unsigned int nbr;
int a;
char *tab;
int i;
i = -1;
nbr = nb;
a = ft_lenght(nbr);
tab = malloc(sizeof(*tab) * a);
while (a != 0)
{
tab[++i] = nbr / a + 48;
nbr = nbr % a;
a = a / 10;
}
tab[++i] = '\0';
return (tab);
}