-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_itoa.c
More file actions
56 lines (51 loc) · 1.49 KB
/
Copy pathft_itoa.c
File metadata and controls
56 lines (51 loc) · 1.49 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jihyjeon < jihyjeon@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/10/19 18:43:21 by jihyjeon #+# #+# */
/* Updated: 2023/11/09 14:29:06 by jihyjeon ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_itoa_base(long long num, char *arr)
{
if (num / 10 != 0)
ft_itoa_base(num / 10, arr - 1);
*arr = num % 10 + '0';
}
void itgr_len(long long num, int *cnt)
{
if (num < 0)
{
num *= -1;
(*cnt)++;
}
if (num / 10 != 0)
{
(*cnt)++;
itgr_len (num / 10, cnt);
}
}
char *ft_itoa(int n)
{
char *arr;
int cnt;
long long long_n;
cnt = 1;
long_n = (long long)n;
itgr_len(long_n, &cnt);
arr = (char *)malloc(sizeof(char) * (cnt + 1));
if (!arr)
return (0);
else if (long_n < 0)
{
*arr = '-';
long_n *= -1;
}
ft_itoa_base(long_n, arr + cnt - 1);
*(arr + cnt) = 0;
return (arr);
}