From 861a36d86e7587c043131228cf3a08afd7bc43ad Mon Sep 17 00:00:00 2001 From: yctct Date: Thu, 5 Feb 2026 11:56:49 +0100 Subject: Add files, first commit --- ft_itoa.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 ft_itoa.c (limited to 'ft_itoa.c') diff --git a/ft_itoa.c b/ft_itoa.c new file mode 100644 index 0000000..f892fb1 --- /dev/null +++ b/ft_itoa.c @@ -0,0 +1,113 @@ +/* +Libft Copyright (C) 2026 yctct + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +#include "libft.h" + +static long int ft_nbrlen(long int n) +{ + int len; + + len = 0; + if (n < 0) + { + n = -n; + len += 1; + } + while (n) + { + n /= 10; + len++; + } + return (len); +} + +char *ft_itoa(int nb) +{ + char *c; + int len; + long n; + + n = nb; + if (n == 0) + return (ft_strdup("0")); + len = ft_nbrlen(n); + c = malloc(sizeof(char) * (len + 1)); + if (!c) + return (NULL); + if (n < 0) + { + n = -n; + c[0] = '-'; + } + c[len] = '\0'; + while (n) + { + c[--len] = (n % 10) + '0'; + n /= 10; + } + return (c); +} +/* +#include +#include + +int main(void) +{ + int n1; + int n2; + int n3; + int n4; + int n5; + char *ptr; + char *ptr2; + char *ptr3; + char *ptr4; + char *ptr5; + char *ptr6; + char c; + + c = 'a'; + n1 = 42; + n2 = 0; + n3 = -42; + n4 = 2147483647; + n5 = -2147483648; + ptr = ft_itoa(n1); + printf(" 42: %s\n", ptr); + free(ptr); + printf("----------------\n"); + ptr2 = ft_itoa(n2); + printf(" 0: %s\n", ptr2); + free(ptr2); + printf("----------------\n"); + ptr3 = ft_itoa(n3); + printf(" -42: %s\n", ptr3); + free(ptr3); + printf("----------------\n"); + ptr4 = ft_itoa(n4); + printf(" INT_MAX: %s\n", ptr4); + free(ptr4); + printf("----------------\n"); + ptr5 = ft_itoa(n5); + printf(" INT_MIN: %s\n", ptr5); + free(ptr5); + printf("----------------\n"); + ptr6 = ft_itoa(c); + printf("char 'a': %s\n", ptr6); + free(ptr6); +} +*/ -- cgit v1.2.3