-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
38 lines (35 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ccline <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/01 17:31:45 by ccline #+# #+# */
/* Updated: 2019/10/05 10:39:42 by ccline ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
int neg;
int intsize;
char *res;
unsigned int nbr;
res = NULL;
nbr = n < 0 ? -n : n;
intsize = ft_intsize(n);
if (!(res = (char *)malloc(sizeof(char) * intsize + 1)))
return (NULL);
neg = n < 0 ? 1 : 0;
res[intsize--] = '\0';
while (intsize >= 0)
{
res[intsize] = nbr % 10 + '0';
nbr /= 10;
intsize--;
}
if (neg == 1)
res[0] = '-';
return (res);
}