-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
62 lines (55 loc) · 1.49 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
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dooh <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/01/20 19:22:34 by dooh #+# #+# */
/* Updated: 2021/01/20 22:39:47 by dooh ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
long long ft_abs(int n)
{
long long num;
num = n;
if (n < 0)
return (num * -1);
return (num);
}
int ft_len(int n)
{
long long num;
int len;
len = 1;
num = ft_abs(n);
while (num /= 10)
len++;
return (len);
}
char *ft_itoa(int n)
{
long long num;
int len;
char *str;
int i;
i = 1;
num = ft_abs(n);
len = ft_len(n);
if (n < 0)
len++;
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
if (n < 0)
str[0] = '-';
while (i <= len)
{
if (str[len - i] != '-')
str[len - i] = (num % 10) + '0';
num /= 10;
i++;
}
str[len] = 0;
return (str);
}