-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
37 lines (34 loc) · 1.34 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lubaujar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2014/11/13 14:20:28 by lubaujar #+# #+# */
/* Updated: 2014/11/13 14:26:32 by lubaujar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itoa(int n)
{
char *ret;
int temp_n;
size_t size_ret;
char sign;
sign = (n < 0) ? -1 : 1;
size_ret = 2 + (n < 0);
temp_n = n;
while ((n = n / 10))
size_ret++;
n = temp_n;
if ((ret = (char *)malloc(sizeof(char) * size_ret--)) == NULL)
return (NULL);
ret[size_ret--] = '\0';
ret[size_ret--] = sign * (n % 10) + '0';
while ((n = n / 10))
ret[size_ret--] = sign * (n % 10) + '0';
if (sign < 0)
ret[size_ret] = '-';
return (ret);
}