-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
61 lines (56 loc) · 1.42 KB
/
ft_itoa.c
File metadata and controls
61 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bguzel <bguzel@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/25 16:59:52 by bguzel #+# #+# */
/* Updated: 2022/10/28 17:33:01 by bguzel ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
int ft_digits(long int nb, int *check)
{
int len;
len = 0;
if (nb == 0)
return (1);
if (nb < 0)
{
nb *= -1;
*check += 1;
len++;
}
while (nb > 0)
{
nb = nb / 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
char *str;
int len;
long num;
int ctr;
num = n;
ctr = 0;
len = ft_digits(num, &ctr);
str = malloc((len + 1) * sizeof(char));
if (!str)
return (NULL);
str[len] = 0;
if (ctr)
{
str[0] = '-';
num *= -1;
}
while (--len >= ctr)
{
str[len] = (num % 10) + '0';
num /= 10;
}
return (str);
}