-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
65 lines (59 loc) · 1.55 KB
/
ft_itoa.c
File metadata and controls
65 lines (59 loc) · 1.55 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
63
64
65
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tdehne <tdehne@student.42heilbronn.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/26 16:08:25 by tdehne #+# #+# */
/* Updated: 2022/04/04 15:52:12 by tdehne ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
static int get_strnum_len(long n)
{
int counter;
counter = 0;
if (!n)
return (1);
if (n < 0)
n *= -1;
while (n > 0)
{
counter++;
n /= 10;
}
return (counter);
}
static long ft_abs(long n)
{
if (n < 0)
return (n * -1);
return (n);
}
char *ft_itoa(int n)
{
char *str;
int len;
int minus;
long num;
minus = 0;
if (n < 0)
minus = 1;
num = ft_abs((long) n);
len = get_strnum_len(num);
if (minus)
len++;
str = (char *) malloc(sizeof(char) * len + 1);
if (!str)
return ((void *) 0);
str[len] = '\0';
while (--len >= 0)
{
str[len] = (num % 10) + '0';
num = num / 10;
if (minus && len == 0)
str[len] = '-';
}
return (str);
}