-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft2_itoa.c
57 lines (52 loc) · 1.46 KB
/
ft2_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft2_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: lpalacio <lpalacio@student.42madrid.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 11:57:59 by lpalacio #+# #+# */
/* Updated: 2022/09/30 12:07:54 by lpalacio ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static unsigned int mem_size(int const n)
{
unsigned int count;
int aux;
count = 1;
aux = n;
if (n <= 0)
count++;
while (aux != 0)
{
count++;
aux = aux / 10;
}
return (count);
}
char *ft_itoa(int n)
{
char *s;
unsigned int count;
count = mem_size (n);
s = (char *)ft_calloc (count, sizeof(char));
if (s == NULL)
return (NULL);
count--;
s[count] = 0;
while (count-- > 0)
{
if (n < 0)
s[count] = '0' - n % 10;
else
s[count] = '0' + n % 10;
n = n / 10;
if (n == 0 && count == 1)
{
s[0] = '-';
count--;
}
}
return (s);
}