59 lines
1.4 KiB
C
59 lines
1.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* ft_itoa.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: adjoly <adjoly@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/11/05 16:26:26 by adjoly #+# #+# */
|
|
/* Updated: 2023/11/06 15:36:28 by adjoly ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "libft.h"
|
|
|
|
static int ft_count_digit(int n)
|
|
{
|
|
int i;
|
|
|
|
i = 0;
|
|
if (n < 0)
|
|
i++;
|
|
if (n == 0)
|
|
return (1);
|
|
while (n)
|
|
{
|
|
n /= 10;
|
|
i++;
|
|
}
|
|
return (i);
|
|
}
|
|
|
|
char *ft_itoa(int n)
|
|
{
|
|
char *result;
|
|
int i;
|
|
|
|
i = ft_count_digit(n);
|
|
if (n == 0)
|
|
return (ft_strdup("0"));
|
|
if (n == -2147483648)
|
|
return (ft_strdup("-2147483648"));
|
|
result = malloc(sizeof(char) * (i + 1));
|
|
if (result == NULL)
|
|
return (NULL);
|
|
result[i--] = '\0';
|
|
if (n < 0)
|
|
{
|
|
result[0] = '-';
|
|
n = -n;
|
|
}
|
|
while (n)
|
|
{
|
|
result[i] = n % 10 + '0';
|
|
n /= 10;
|
|
i--;
|
|
}
|
|
return (result);
|
|
}
|