libft/ft_itoa.c
Etienne Rey-bethbeder 191a193cb2 origin
2022-10-11 22:51:06 +02:00

43 lines
1.3 KiB
C

/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: erey-bet <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 17:12:52 by erey-bet #+# #+# */
/* Updated: 2022/10/10 22:15:30 by erey-bet ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
char *str;
int len;
int i;
long nl;
i = 0;
nl = n;
nl *= (n > 0) - (n < 0);
len = ft_get_size(nl);
if (n < 0)
{
str = malloc(len + 2);
if (str == NULL)
return (NULL);
str[i++] = '-';
}
else
str = malloc(len + 1);
if (str == NULL)
return (NULL);
while (len > 1)
str[i++] = (nl / ft_power(10, --len) % 10) + 48;
str[i++] = nl % 10 + 48;
str[i] = '\0';
return (str);
}