itoa fonctionne pour les long et creation de utoa
This commit is contained in:
1
Makefile
1
Makefile
@@ -43,6 +43,7 @@ SRCS = ft_memset.c \
|
||||
ft_strtrim.c \
|
||||
ft_split.c \
|
||||
ft_itoa.c \
|
||||
ft_utoa.c \
|
||||
ft_strmapi.c \
|
||||
ft_putchar_fd.c \
|
||||
ft_putstr_fd.c \
|
||||
|
||||
1
libft.h
1
libft.h
@@ -46,6 +46,7 @@ char *ft_strjoin(char const *s1, char const *s2);
|
||||
char *ft_strtrim(char const *s1, char const *set);
|
||||
char **ft_split(char const *s, char c);
|
||||
char *ft_itoa(long int n);
|
||||
char *ft_utoa(unsigned long int n);
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
void ft_putchar_fd(char c, int fd);
|
||||
void ft_putstr_fd(char *s, int fd);
|
||||
|
||||
63
srcs/ft_utoa.c
Normal file
63
srcs/ft_utoa.c
Normal 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);
|
||||
}
|
||||
Reference in New Issue
Block a user