-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
42 lines (39 loc) · 1.29 KB
/
ft_itoa.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lroussel <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/11/07 15:48:24 by lroussel #+# #+# */
/* Updated: 2024/11/07 17:18:15 by lroussel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
unsigned int size;
char *res;
if (n == MIN_INT)
return (ft_strdup("-2147483648"));
size = ft_count_digits(n);
if (n < 0)
size++;
res = malloc(sizeof(char) * (size + 1));
if (!res)
return (NULL);
if (n < 0)
{
res[0] = '-';
n *= -1;
}
res[size--] = '\0';
while (n > 9)
{
res[size] = (n % 10) + '0';
n /= 10;
size--;
}
res[size] = n + '0';
return (res);
}