-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_utoa.c
More file actions
63 lines (56 loc) · 1.57 KB
/
ft_utoa.c
File metadata and controls
63 lines (56 loc) · 1.57 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aobshatk <aobshatk@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/10 09:22:15 by aobshatk #+# #+# */
/* Updated: 2025/01/09 11:16:46 by aobshatk ### ########.fr */
/* */
/* ************************************************************************** */
#include "libftprintf.h"
static int intsize(long long int n)
{
int len;
len = 0;
if (n == 0)
len = 1;
while (n > 0)
{
len++;
n /= 10;
}
return (len);
}
static char *insertnum(long long int n, size_t size)
{
int len;
char *strint;
len = size - 1;
strint = NULL;
strint = malloc(size + 1);
if (strint == NULL)
return (NULL);
if (n == 0)
strint[len] = '0';
while (len >= 0 && n > 0)
{
strint[len] = n % 10 + 48;
n = n / 10;
len--;
}
strint[size] = '\0';
return (strint);
}
char *ft_utoa(unsigned int n)
{
long long int num;
int len;
char *result;
len = 0;
num = (long long int)n;
len += intsize(num);
result = insertnum(num, sizeof(char) * len);
return (result);
}