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.
pipex/libft/str/ft_itoa.c

59 lines
1.4 KiB
C
Raw Normal View History

2024-04-11 14:02:42 +02:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adjoly <adjoly@student.42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/05 16:26:26 by adjoly #+# #+# */
/* Updated: 2024/02/04 14:45:39 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);
}