-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
60 lines (54 loc) · 1.45 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rdragan <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/05 16:27:11 by rdragan #+# #+# */
/* Updated: 2022/11/05 16:38:12 by rdragan ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t number_len(int n)
{
size_t len;
len = 0;
if (n <= 0)
len++;
while (n)
{
len++;
n = n / 10;
}
return (len);
}
static int abs_val(int a)
{
if (a < 0)
return (-a);
return (a);
}
/*
Returns a string representing the integer received.
*/
char *ft_itoa(int n)
{
char *new;
size_t len;
len = number_len(n);
new = malloc((len + 1) * sizeof(char));
if (!new)
return (NULL);
new[len--] = '\0';
if (n == 0)
new[0] = '0';
if (n < 0)
new[0] = '-';
while (n)
{
new[len--] = abs_val(n % 10) + '0';
n = n / 10;
}
return (new);
}