-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_itoa.c
55 lines (50 loc) · 1.33 KB
/
ft_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: oelkhiar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/20 16:51:50 by oelkhiar #+# #+# */
/* Updated: 2022/10/21 10:31:40 by oelkhiar ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static size_t ft_numlen(int n)
{
size_t len;
len = 0;
if (n <= 0)
len++;
while (n)
{
n /= 10;
len++;
}
return (len);
}
char *ft_itoa(int n)
{
int len;
char *str;
long long a;
len = ft_numlen(n);
str = malloc(len + 1);
a = n;
if (!str)
return (0);
if (a < 0)
{
str[0] = '-';
a *= -1;
}
if (a == 0)
str[0] = '0';
str[len--] = '\0';
while (a)
{
str[len--] = ((a % 10) + 48);
a /= 10;
}
return (str);
}