-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
65 lines (58 loc) · 1.68 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
63
64
65
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jovicto2 <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/25 15:04:52 by jovicto2 #+# #+# */
/* Updated: 2023/05/25 15:13:51 by jovicto2 ### ########.fr */
/* */
/* ************************************************************************** */
#include "../includes/libft.h"
static size_t ft_ilen(long number);
static void ft_putnbr_str(long n, size_t i, size_t len, char *str);
char *ft_itoa(int n)
{
long number;
size_t length;
char *string;
number = n;
length = ft_ilen(number);
string = ft_calloc(length + 1, sizeof(char));
if (!string)
return (NULL);
if (number < 0)
{
number *= -1;
*string = '-';
ft_putnbr_str(number, 1, length, string);
}
else
ft_putnbr_str(number, 0, length, string);
return (string);
}
static size_t ft_ilen(long number)
{
size_t counter;
counter = 0;
if (number <= 0)
{
number *= -1;
counter++;
}
while (number)
{
number /= 10;
counter++;
}
return (counter);
}
static void ft_putnbr_str(long n, size_t i, size_t len, char *str)
{
while (i < len)
{
str[--len] = (n % 10) + '0';
n /= 10;
}
}