-
Notifications
You must be signed in to change notification settings - Fork 2
/
ft_itoa.c
40 lines (37 loc) · 1.28 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hbaddrul <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/05/07 20:03:01 by hbaddrul #+# #+# */
/* Updated: 2021/11/21 20:24:19 by hbaddrul ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "libft.h"
char *ft_itoa(int n)
{
int len;
char *ret;
const char *digits = "0123456789";
len = ft_numlen(n, 10);
ret = malloc(sizeof(char) * (len + 1));
if (!ret)
return (0);
ret[len] = 0;
if (n == 0)
ret[0] = '0';
if (n < 0)
ret[0] = '-';
while (n)
{
if (n > 0)
ret[--len] = digits[n % 10];
else
ret[--len] = digits[n % 10 * -1];
n /= 10;
}
return (ret);
}