diff options
| author | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-21 16:29:51 +0100 |
|---|---|---|
| committer | Thomas Vanbesien <tvanbesi@proton.me> | 2026-02-21 16:29:51 +0100 |
| commit | a8f7e6e83bc4846c94c0489e0f6e0f807b35073e (patch) | |
| tree | 0d245c73490427a39d4338da483e335a267c6917 /src/ft_itoa.c | |
| parent | fd62678a9cf38e3f70efbdd093c1012d448548e1 (diff) | |
| download | Libft-a8f7e6e83bc4846c94c0489e0f6e0f807b35073e.tar.gz Libft-a8f7e6e83bc4846c94c0489e0f6e0f807b35073e.zip | |
Implement libft Part 2 with tests
Add ft_substr, ft_strjoin, ft_strtrim, ft_split, ft_itoa,
ft_strmapi, ft_striteri, ft_putchar_fd, ft_putstr_fd,
ft_putendl_fd, ft_putnbr_fd.
Diffstat (limited to 'src/ft_itoa.c')
| -rw-r--r-- | src/ft_itoa.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/ft_itoa.c b/src/ft_itoa.c new file mode 100644 index 0000000..05f3fae --- /dev/null +++ b/src/ft_itoa.c @@ -0,0 +1,45 @@ +#include "libft.h" +#include <stdlib.h> + +static size_t +_s_numlen (int n) +{ + size_t len; + + len = 1; + if (n < 0) + len++; + while (n / 10) + { + len++; + n /= 10; + } + return (len); +} + +char * +ft_itoa (int n) +{ + size_t len; + char *str; + unsigned int nb; + + len = _s_numlen (n); + str = malloc (len + 1); + if (!str) + return (NULL); + str[len] = '\0'; + if (n < 0) + { + str[0] = '-'; + nb = -n; + } + else + nb = n; + while (len > 0 && str[len - 1] != '-') + { + str[--len] = '0' + nb % 10; + nb /= 10; + } + return (str); +} |
