-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa.c
56 lines (51 loc) · 1.47 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: chonorat <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/05 14:33:41 by chonorat #+# #+# */
/* Updated: 2022/12/05 15:42:06 by chonorat ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static long int count_int(int n)
{
long int count;
count = 1;
while (n > 9 || n < -9)
{
count++;
n /= 10;
}
if (n < 0)
count++;
return (count);
}
char *ft_itoa(int n)
{
unsigned int lenght;
long int number;
char *char_n;
number = n;
lenght = count_int(n);
char_n = (char *)malloc((lenght + 1) * sizeof(char));
if (!char_n)
return (NULL);
char_n[lenght] = '\0';
if (number < 0)
{
char_n[0] = '-';
number *= -1;
}
while (number > 0)
{
lenght--;
char_n[lenght] = (number % 10) + '0';
number /= 10;
}
if (n == 0)
char_n[0] = '0';
return (char_n);
}