-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_uitostr.c
56 lines (51 loc) · 1.47 KB
/
ft_uitostr.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
56
/* ************************************************************************** */
/* */
/* :::::::: */
/* ft_uitostr.c :+: :+: */
/* +:+ */
/* By: splattje <[email protected]> +#+ */
/* +#+ */
/* Created: 2023/11/08 14:09:21 by splattje #+# #+# */
/* Updated: 2023/11/08 16:26:34 by splattje ######## odam.nl */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int get_length(unsigned int number)
{
int length;
if (number == 0)
length = 1;
else
length = 0;
while (number > 0)
{
number /= 10;
length++;
}
return (length);
}
char *uitostr(unsigned int num)
{
int num_digit;
char *str;
int i;
num_digit = get_length(num);
str = (char *)malloc((num_digit + 1) * sizeof(char));
if (str == NULL)
return (NULL);
if (num == 0)
{
str[0] = '0';
str[1] = '\0';
return (str);
}
i = num_digit - 1;
while (i >= 0)
{
str[i] = '0' + (num % 10);
num /= 10;
i--;
}
str[num_digit] = '\0';
return (str);
}