-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
51 lines (46 loc) · 1.35 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
43
44
45
46
47
48
49
50
51
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: yjouaoud <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/04/08 20:37:12 by yjouaoud #+# #+# */
/* Updated: 2019/04/13 15:27:21 by yjouaoud ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_ccc(int n)
{
int tmp;
int size;
tmp = n;
size = 1;
while ((tmp = tmp / 10))
size++;
return (size);
}
char *ft_itoa(int n)
{
char *a;
int size;
int sign;
sign = 0;
if (n < 0)
sign = 1;
size = ft_ccc(n);
a = ft_strnew(size + sign);
if (a == NULL)
return (NULL);
if (sign)
a[0] = '-';
while (size--)
{
if (sign == 1)
a[size + sign] = -(n % 10) + '0';
else
a[size + sign] = (n % 10) + '0';
n /= 10;
}
return (a);
}