-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
55 lines (50 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adiaz-be <adiaz-be@student.42malaga.c +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 17:51:48 by adiaz-be #+# #+# */
/* Updated: 2022/09/30 17:52:15 by adiaz-be ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t get_size(int n)
{
size_t size;
if (n > 0)
size = 0;
else
size = 1;
while (n)
{
n /= 10;
size++;
}
return (size);
}
char *ft_itoa(int n)
{
char *str;
long num;
size_t size;
num = n;
size = get_size(n);
if (n < 0)
num *= -1;
str = (char *)malloc(size + 1);
if (!str)
return (NULL);
*(str + size--) = '\0';
while (num > 0)
{
*(str + size--) = num % 10 + '0';
num /= 10;
}
if (size == 0 && str[1] == '\0')
*(str + size) = '0';
else if (size == 0 && str[1])
*(str + size) = '-';
return (str);
}