59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_itoa.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: pbonilla <eirodeis.lepnj@gmail.com> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2020/11/24 21:27:16 by pbonilla #+# #+# */
|
|
/* Updated: 2021/01/31 04:53:05 by pbonilla ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
int number_len(int n)
|
|
{
|
|
int size;
|
|
|
|
if (n == 0)
|
|
return (1);
|
|
size = 0;
|
|
if (n < 0)
|
|
{
|
|
n = -n;
|
|
++size;
|
|
}
|
|
while (n != 0)
|
|
{
|
|
n /= 10;
|
|
++size;
|
|
}
|
|
return (size);
|
|
}
|
|
|
|
char *ft_itoa(int n)
|
|
{
|
|
long long_n;
|
|
char *number;
|
|
int size;
|
|
|
|
long_n = n;
|
|
size = number_len(n);
|
|
if (!(number = malloc(sizeof(char) * (size + 1))))
|
|
return (NULL);
|
|
number[size] = 0;
|
|
--size;
|
|
if (long_n < 0)
|
|
long_n = -long_n;
|
|
while (size >= 0)
|
|
{
|
|
number[size] = '0' + (long_n % 10);
|
|
--size;
|
|
long_n /= 10;
|
|
}
|
|
if (n < 0)
|
|
number[0] = '-';
|
|
return (number);
|
|
}
|