-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_utoa.c
49 lines (44 loc) · 1.35 KB
/
ft_utoa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sprodatu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 22:43:27 by sprodatu #+# #+# */
/* Updated: 2024/05/03 21:51:53 by sprodatu ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft/libft.h"
#include "ft_printf.h"
int ft_unumlen(unsigned int n)
{
unsigned int count;
count = 0;
if (n == 0)
return (1);
while (n > 0)
{
n /= 10;
count++;
}
return (count);
}
char *ft_utoa(unsigned int n)
{
char *str;
int len;
len = ft_unumlen(n);
str = (char *)malloc(sizeof(char) * (len + 1));
if (!str)
return (NULL);
str[len] = '\0';
if (n == 0)
str[0] = '0';
while (n != 0)
{
str[--len] = (n % 10) + '0';
n /= 10;
}
return (str);
}