itoa fonctionne pour les long et creation de utoa

This commit is contained in:
hugodu69
2020-02-14 12:02:46 +01:00
parent d9723712ae
commit d7dd6e8ba8
3 changed files with 65 additions and 0 deletions

63
srcs/ft_utoa.c Normal file
View File

@@ -0,0 +1,63 @@
/*
** take an unsigned integer and give a string
*/
/*
** #include <stdio.h> // for printf
** #include <stdlib.h> // for atoi
**
** char *ft_utoa(unsigned long int n);
**
** int main(int ac, char **av)
** {
** if (ac == 0)
** return (0);
** else if (ac == 2)
** printf("%s\n",ft_utoa(atoi(av[1])));
** else
** {
** unsigned long int i;
** i = 0;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 237683;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 1234567;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 12345678;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 2147483646;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 2147483647;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 2147483648;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 2147483649;
** printf(" %lu\n %s\n\n",i,ft_utoa(i));
** i = 9223372036854775807;
** printf("| %lu\n| %s\n\n",i,ft_utoa(i));
** }
** return 0;
** }
*/
#include "libft.h"
char *ft_utoa(unsigned long int n)
{
char *str;
int len;
unsigned long int cpy;
cpy = n;
len = 1;
while (n /= 10)
len++;
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
str[len] = '\0';
str[--len] = cpy % 10 + '0';
while (cpy /= 10)
str[--len] = cpy % 10 + '0';
return (str);
}