You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

55 lines
1.3 KiB

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emartine <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/08 15:29:04 by emartine #+# #+# */
/* Updated: 2017/11/08 15:29:05 by emartine ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_itoa_len(int n)
{
size_t r;
if (n == 0)
return (1);
r = 0;
if (n < 0)
r++;
while (n)
{
n /= 10;
r++;
}
return (r);
}
char *ft_itoa(int n)
{
size_t l;
size_t e;
char *r;
l = ft_itoa_len(n);
if (!(r = (char *)malloc(l + 1)))
return (NULL);
r[l] = '\0';
if (n < 0)
{
r[0] = '-';
e = 1;
}
else
e = 0;
while (l-- > e)
{
r[l] = '0' + n % 10 * (n < 0 ? -1 : 1);
n /= 10;
}
return (r);
}