-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
42 lines (39 loc) · 1.33 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: yochered <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/10/24 11:31:35 by yochered #+# #+# */
/* Updated: 2018/10/24 11:32:05 by yochered ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include <libft.h>
char *ft_itoa(int n)
{
char *res;
int len;
int start;
len = ft_count_digits(n, 10);
start = 0;
res = (char *)malloc((len + 1) * sizeof(char));
if (!res)
return (NULL);
if (n < 0)
{
start++;
res[0] = '-';
}
res[len--] = '\0';
while (len >= start)
{
if (n > 0)
res[len--] = n % 10 + 48;
else
res[len--] = -(n % 10) + 48;
n /= 10;
}
return (res);
}