Archived
1
0
This repository has been archived on 2024-10-25. You can view files and clone it, but cannot push or open issues or pull requests.
libft/ft_itoa.c
Adam Joly 52e89b462c fclean
2023-11-08 12:47:17 +01:00

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);
}