-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_itoa_u.c
44 lines (39 loc) · 1.32 KB
/
ft_itoa_u.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa_u.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sanmetol <sanmetol@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/19 12:51:28 by sanmetol #+# #+# */
/* Updated: 2023/10/18 19:54:40 by sanmetol ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
static int count_digits(unsigned int nbr)
{
unsigned int count;
count = 1;
while (nbr >= 10)
{
count++;
nbr = nbr / 10;
}
return (count);
}
char *ft_itoa_u(unsigned int n)
{
int digits;
char *str;
digits = count_digits(n);
str = (char *)malloc((digits + 1) * sizeof(char));
if (str == NULL)
return (NULL);
str[digits] = '\0';
while (digits--)
{
str[digits] = (n % 10) + '0';
n = n / 10;
}
return (str);
}