-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
55 lines (50 loc) · 1.38 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
52
53
54
55
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: juchoi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/21 15:58:15 by juchoi #+# #+# */
/* Updated: 2021/01/21 20:44:27 by juchoi ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int ft_intlen(int n)
{
int i;
i = 0;
if (n <= 0)
i = i + 1;
while (n != 0)
{
n = n / 10;
i++;
}
return (i);
}
char *ft_itoa(int n)
{
char *str;
int len;
int i;
long long ln;
ln = (long long)n;
len = ft_intlen(n);
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (0);
str[len--] = '\0';
i = 0;
if (ln < 0)
{
ln = ln * -1;
str[i++] = '-';
}
while (i <= len)
{
str[len] = (ln % 10) + '0';
ln = ln / 10;
len--;
}
return (str);
}