aboutsummaryrefslogtreecommitdiff
path: root/src/str/ft_itoa_cpy.c
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-09-14 16:01:13 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-09-14 16:01:13 +0200
commit3133f0d4d640abd62287187d13d380d03cce00a7 (patch)
tree1c1f9c83047559aa543b0839d33a945c38a13649 /src/str/ft_itoa_cpy.c
parent50876fe6b9e369d6b51bac9fa62b790ef5bda9d7 (diff)
downloadlibft-3133f0d4d640abd62287187d13d380d03cce00a7.tar.gz
libft-3133f0d4d640abd62287187d13d380d03cce00a7.tar.bz2
libft-3133f0d4d640abd62287187d13d380d03cce00a7.zip
Added ft_itoa_cpy
Diffstat (limited to 'src/str/ft_itoa_cpy.c')
-rw-r--r--src/str/ft_itoa_cpy.c44
1 files changed, 44 insertions, 0 deletions
diff --git a/src/str/ft_itoa_cpy.c b/src/str/ft_itoa_cpy.c
new file mode 100644
index 0000000..a66d016
--- /dev/null
+++ b/src/str/ft_itoa_cpy.c
@@ -0,0 +1,44 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_itoa_cpy.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2019/10/07 10:19:56 by cacharle #+# #+# */
+/* Updated: 2020/09/14 15:59:28 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+/*
+** \brief itoa but cpy number in a buffer instead of allocating memory
+** \param dst Buffer where to put the string representation of the number
+** \param n Number to convert
+** \return Always dst
+*/
+
+char *ft_itoa_cpy(char *dst, int n)
+{
+ int len;
+ unsigned int u_nbr;
+
+ len = n < 0 || n == 0 ? 1 : 0;
+ u_nbr = n < 0 ? -n : n;
+ while (u_nbr > 0)
+ {
+ u_nbr /= 10;
+ len++;
+ }
+ dst[len] = '\0';
+ u_nbr = n < 0 ? -n : n;
+ if (n < 0)
+ dst[0] = '-';
+ while (--len >= (n < 0 ? 1 : 0))
+ {
+ dst[len] = (u_nbr % 10) | 0x30;
+ u_nbr /= 10;
+ }
+ return (dst);
+}