-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_unsigned.c
More file actions
88 lines (78 loc) · 1.83 KB
/
Copy pathft_unsigned.c
File metadata and controls
88 lines (78 loc) · 1.83 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_unsigned.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: atok <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/11/05 17:28:17 by atok #+# #+# */
/* Updated: 2022/11/10 11:05:14 by atok ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_printf.h"
int unsignedint_len(unsigned int n)
{
int len;
len = 0;
while (n > 0)
{
n = n / 10;
len++;
}
return (len);
}
char *ft_ustrdup(const char *src)
{
char *dup;
int i;
int len;
len = 0;
while (src[len])
len++;
dup = (char *) malloc(sizeof(char) * (len + 1));
if (dup == NULL)
return (NULL);
i = 0;
while (src[i] != 0x00)
{
dup[i] = src[i];
i++;
}
dup[i] = 0x00;
return (dup);
}
char *ft_uint_itoa(unsigned int n)
{
char *str;
int i;
if (n == 0)
return (ft_ustrdup("0"));
i = unsignedint_len(n);
str = (char *) malloc(sizeof(char) * (i + 1));
if (str == NULL)
return (NULL);
str[i] = 0x00;
i--;
while (n != 0)
{
str[i] = '0' + (n % 10);
i--;
n = n / 10;
}
return (str);
}
int ft_unsigned(unsigned int n)
{
int i;
char *str;
i = 0;
str = ft_uint_itoa(n);
while (str[i] != 0x00)
{
write(1, &str[i], 1);
i++;
}
free (str);
return (i);
}
/* basically same as int but without the sign '-' */