Matthos Denys 5f1937beec update
2021-05-18 21:36:56 +03:00

44 lines
598 B
C

#include "../includes/philo_header.h"
static int nbleng(unsigned int n)
{
unsigned int i;
i = 0;
while (n >= 10)
{
n /= 10;
i++;
}
return (i + 1);
}
char *ft_itoa(int n)
{
char *dst;
unsigned int leng;
unsigned int nbr;
unsigned int i;
nbr = (n < 0 ? -n : n);
leng = nbleng(nbr);
i = 0;
if (!(dst = (char *)malloc(sizeof(char) * leng + 1 + (n < 0 ? 1 : 0))))
return (NULL);
if (n < 0)
{
dst[i] = '-';
leng++;
}
i = leng - 1;
while (nbr >= 10)
{
dst[i] = nbr % 10 + '0';
nbr /= 10;
i--;
}
dst[i] = nbr % 10 + '0';
dst[leng] = '\0';
return (dst);
}