-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathft_itoa.c
More file actions
62 lines (57 loc) · 1.46 KB
/
ft_itoa.c
File metadata and controls
62 lines (57 loc) · 1.46 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* itoa_main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jony <jony@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/30 14:53:53 by mhasan #+# #+# */
/* Updated: 2019/11/04 21:22:54 by jony ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int lencal(long n)
{
int result;
result = 0;
if (n < 0)
{
n = n * -1;
result++;
}
if (n == 0)
return (result + 1);
while (n > 0)
{
n = n / 10;
result++;
}
return (result);
}
char *ft_itoa(int n)
{
int len;
char *ptr;
long nbr;
nbr = n;
len = lencal(nbr);
if (!(ptr = (char *)malloc(sizeof(*ptr) * (len + 1))))
return (NULL);
ptr[len--] = '\0';
if (nbr == 0)
{
ptr[0] = '0';
return (ptr);
}
if (nbr < 0)
{
ptr[0] = '-';
nbr = nbr * -1;
}
while (nbr > 0)
{
ptr[len--] = (nbr % 10) + '0';
nbr = nbr / 10;
}
return (ptr);
}