-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
62 lines (56 loc) · 1.42 KB
/
Copy pathft_itoa.c
File metadata and controls
62 lines (56 loc) · 1.42 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: admansar <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/16 14:18:24 by admansar #+# #+# */
/* Updated: 2022/10/20 18:56:54 by admansar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
long int ft_counter(int k)
{
int i;
i = 0;
if (k <= 0)
i++;
while (k != 0)
{
i++;
k = k / 10;
}
return (i);
}
void converter(char *p, long int i, long int a)
{
while (a != 0)
{
p[i] = (a % 10) + 48;
i--;
a = a / 10;
}
}
char *ft_itoa(int n)
{
long int j;
char *ptr;
long int s;
s = (long int)n;
j = ft_counter(s);
ptr = malloc(j + 1);
if (!ptr)
return (0);
ptr[j] = '\0';
j--;
if (s == 0)
ptr[0] = '0';
else if (s < 0)
{
s = (-1) * s;
ptr[0] = '-';
}
converter(ptr, j, s);
return (ptr);
}