-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_utoa.c
More file actions
73 lines (63 loc) · 1.76 KB
/
Copy pathft_utoa.c
File metadata and controls
73 lines (63 loc) · 1.76 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: joleksia <joleksia@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/13 08:11:36 by joleksia #+# #+# */
/* Updated: 2024/12/22 16:04:47 by joleksia ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_utils.h"
#include "./libft/libft.h"
static int fts_numlen(unsigned int n);
static char *fts_strproc(char *s, unsigned int n);
static char *fts_strrev(char *s, size_t len);
char *ft_utoa(unsigned int u)
{
char *result;
result = (char *) ft_calloc(fts_numlen(u) + 1, sizeof(char));
if (!result)
return (NULL);
result = fts_strproc(result, u);
return (result);
}
static int fts_numlen(unsigned int n)
{
int result;
result = 1;
while (n)
{
n /= 10;
if (n)
result++;
}
return (result);
}
static char *fts_strproc(char *s, unsigned int n)
{
char *scpy;
scpy = s;
if (n == 0)
*s++ = '0';
while (n)
{
*s++ = n % 10 + '0';
n /= 10;
}
return (fts_strrev(scpy, ft_strlen(scpy)));
}
static char *fts_strrev(char *s, size_t len)
{
size_t i;
char temp;
i = -1;
while (++i < len / 2)
{
temp = s[i];
s[i] = s[len - 1 - i];
s[len - 1 - i] = temp;
}
return (s);
}