-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
56 lines (51 loc) · 1.51 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jlinarez <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/25 08:35:01 by jlinarez #+# #+# */
/* Updated: 2024/03/25 08:35:18 by jlinarez ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static unsigned int ft_number_size(int number)
{
unsigned int length;
length = 0;
if (number <= 0)
length = 1;
else
length = 0;
while (number != 0)
{
number /= 10;
length++;
}
return (length);
}
char *ft_itoa(int n)
{
unsigned int length;
char *str;
unsigned long number;
length = ft_number_size(n);
str = (char *)malloc(sizeof(char) * (length + 1));
if (!str)
return (NULL);
str[length] = '\0';
number = 0;
if (n < 0)
number = -(long)n;
else
number = n;
while (length > 0)
{
str[--length] = (number % 10) + '0';
number /= 10;
}
if (n < 0 && length == 0)
str[0] = '-';
return (str);
}