-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
58 lines (53 loc) · 1.46 KB
/
ft_itoa.c
File metadata and controls
58 lines (53 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmetehri <bmetehri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/11 12:52:04 by bmetehri #+# #+# */
/* Updated: 2023/01/05 19:05:35 by bmetehri ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void do_stuff(long n, char *str, long *length)
{
long nb;
if (n < 0)
{
str[0] = '-';
nb = -n;
}
else
nb = n;
if (nb >= 0 && nb <= 9)
str[*length] = nb + 48;
if (nb > 9)
{
do_stuff(nb % 10, str, length);
(*length)--;
do_stuff(nb / 10, str, length);
}
}
char *ft_itoa(int n)
{
long nb;
long length;
char *str;
length = 0;
if (n <= 0)
length++;
nb = n;
while (nb)
{
nb /= 10;
length++;
}
str = malloc((length + 1) * sizeof(char));
if (!str)
return (NULL);
str[length] = '\0';
length--;
do_stuff((long)n, str, &length);
return (str);
}