Browse Source

added printf functions

master
narnaud 3 years ago
parent
commit
0ba7ffd7c6
  1. 3
      Makefile
  2. 37
      ft_itoa.c
  3. 35
      ft_itox.c
  4. 50
      ft_nbrlen.c
  5. 32
      ft_strrev.c
  6. 33
      ft_utoa.c
  7. 8
      libft.h

3
Makefile

@ -5,7 +5,8 @@ SRCS = ft_isalpha.c ft_isdigit.c ft_isascii.c ft_isprint.c ft_isalnum.c \
ft_strnstr.c ft_strdup.c ft_toupper.c ft_tolower.c ft_atoi.c ft_bzero.c \ ft_strnstr.c ft_strdup.c ft_toupper.c ft_tolower.c ft_atoi.c ft_bzero.c \
ft_memset.c ft_memchr.c ft_memcpy.c ft_memcmp.c ft_memmove.c ft_calloc.c \ ft_memset.c ft_memchr.c ft_memcpy.c ft_memcmp.c ft_memmove.c ft_calloc.c \
ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c \ ft_substr.c ft_strjoin.c ft_strtrim.c ft_split.c ft_itoa.c ft_strmapi.c \
ft_striteri.c ft_putchar_fd.c ft_putnbr_fd.c ft_putendl_fd.c ft_putstr_fd.c ft_striteri.c ft_putchar_fd.c ft_putnbr_fd.c ft_putendl_fd.c ft_putstr_fd.c \
ft_nbrlen.c ft_strrev.c ft_itox.c ft_utoa.c
SRCS_BONUS = ft_lstsize_bonus.c ft_lstnew_bonus.c ft_lstlast_bonus.c \ SRCS_BONUS = ft_lstsize_bonus.c ft_lstnew_bonus.c ft_lstlast_bonus.c \
ft_lstadd_back_bonus.c ft_lstadd_front_bonus.c ft_lstdelone_bonus.c \ ft_lstadd_back_bonus.c ft_lstadd_front_bonus.c ft_lstdelone_bonus.c \

37
ft_itoa.c

@ -6,45 +6,12 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/20 15:50:47 by narnaud #+# #+# */ /* Created: 2021/10/20 15:50:47 by narnaud #+# #+# */
/* Updated: 2021/11/15 12:10:47 by narnaud ### ########.fr */ /* Updated: 2021/12/21 09:59:19 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "libft.h" #include "libft.h"
static void ft_strrev(char **str, unsigned int neg)
{
char ch;
size_t size;
size_t i;
size = ft_strlen(*str);
if (neg)
(*str)[size++] = '-';
i = 0;
while (i < size / 2)
{
ch = (*str)[i];
(*str)[i] = (*str)[size - i - 1];
(*str)[size - i - 1] = ch;
i++;
}
}
static size_t get_nbrlength(int nbr)
{
size_t i;
i = 0;
if (nbr == 0)
return (1);
else if (nbr < 0)
i++;
while (nbr && ++i)
nbr = nbr / 10;
return (i);
}
char *ft_itoa(int n) char *ft_itoa(int n)
{ {
char *ret; char *ret;
@ -57,7 +24,7 @@ char *ft_itoa(int n)
return (ft_strdup("0")); return (ft_strdup("0"));
i = 0; i = 0;
neg = 0; neg = 0;
ret = ft_calloc(get_nbrlength(n) + 1, sizeof(char)); ret = ft_calloc(ft_ilen(n) + 1, sizeof(char));
if (!ret) if (!ret)
return (NULL); return (NULL);
if (n < 0) if (n < 0)

35
ft_itox.c

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itox.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/21 09:51:26 by narnaud #+# #+# */
/* Updated: 2021/12/21 09:56:39 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_itox(unsigned long int n, const char *base)
{
char *ret;
size_t i;
unsigned int neg;
size_t size;
size = ft_longbaselen(n, 16);
if (n == 0)
return (ft_strdup("0"));
i = 0;
neg = 0;
ret = ft_calloc(size + 1, sizeof(char));
while (n)
{
ret[i++] = base[n % 16];
n = n / 16;
}
ft_strrev(&ret, neg);
return (ret);
}

50
ft_nbrlen.c

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nbrlen.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/21 09:42:43 by narnaud #+# #+# */
/* Updated: 2021/12/21 09:53:16 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_ilen(int nbr)
{
size_t i;
i = 0;
if (nbr == 0)
return (1);
else if (nbr < 0)
i++;
while (nbr && ++i)
nbr = nbr / 10;
return (i);
}
size_t ft_ulen(unsigned int nbr)
{
size_t i;
i = 0;
while (nbr && ++i)
nbr = nbr / 10;
return (i);
}
size_t ft_longbaselen(long nbr, size_t base)
{
size_t i;
if (nbr <= 0)
i = 1;
else
i = 0;
while (nbr && ++i)
nbr = nbr / base;
return (i);
}

32
ft_strrev.c

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strrev.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/27 09:36:21 by narnaud #+# #+# */
/* Updated: 2021/10/27 09:52:46 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
void ft_strrev(char **str, unsigned int neg)
{
char ch;
size_t size;
size_t i;
size = ft_strlen(*str);
if (neg)
(*str)[size++] = '-';
i = 0;
while (i < size / 2)
{
ch = (*str)[i];
(*str)[i] = (*str)[size - i - 1];
(*str)[size - i - 1] = ch;
i++;
}
}

33
ft_utoa.c

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* utoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/12/21 09:48:38 by narnaud #+# #+# */
/* Updated: 2021/12/21 09:49:12 by narnaud ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
char *ft_utoa(unsigned int n)
{
char *ret;
unsigned int i;
if (n == 4294967295)
return (ft_strdup("4294967295"));
if (n == 0)
return (ft_strdup("0"));
i = 0;
ret = ft_calloc(ft_ulen(n) + 1, sizeof(char));
while (n)
{
ret[i++] = (n % 10) + '0';
n = n / 10;
}
ft_strrev(&ret, 0);
return (ret);
}

8
libft.h

@ -6,7 +6,7 @@
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ /* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ /* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */
/* Updated: 2021/11/15 13:07:44 by narnaud ### ########.fr */ /* Updated: 2021/12/21 10:02:24 by narnaud ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -35,6 +35,9 @@ void *ft_memchr(const void *b, int c, size_t n);
int ft_memcmp(const void *s1, const void *s2, size_t n); int ft_memcmp(const void *s1, const void *s2, size_t n);
void *ft_memcpy(void *dst, const void *src, size_t n); void *ft_memcpy(void *dst, const void *src, size_t n);
void *ft_memmove(void *dst, const void *src, size_t n); void *ft_memmove(void *dst, const void *src, size_t n);
size_t ft_ilen(int nbr);
size_t ft_ulen(unsigned int nbr);
size_t ft_longbaselen(long nbr, size_t base);
void *ft_memset(void *b, int c, size_t len); void *ft_memset(void *b, int c, size_t len);
char *ft_strchr(const char *s, int c); char *ft_strchr(const char *s, int c);
char *ft_strdup(const char *src); char *ft_strdup(const char *src);
@ -44,6 +47,7 @@ size_t ft_strlen(const char *s);
int ft_strncmp(const char *s1, const char *s2, size_t n); int ft_strncmp(const char *s1, const char *s2, size_t n);
char *ft_strnstr(const char *haystack, const char *needle, size_t len); char *ft_strnstr(const char *haystack, const char *needle, size_t len);
char *ft_strrchr(const char *s, int c); char *ft_strrchr(const char *s, int c);
void ft_strrev(char **str, unsigned int neg);
int ft_tolower(int c); int ft_tolower(int c);
int ft_toupper(int c); int ft_toupper(int c);
char *ft_substr(char const *s, unsigned int start, size_t len); char *ft_substr(char const *s, unsigned int start, size_t len);
@ -51,6 +55,8 @@ char *ft_strjoin(const char *s1, const char *s2);
char *ft_strtrim(char const *s1, char const *set); char *ft_strtrim(char const *s1, char const *set);
char **ft_split(char const *str, char ch); char **ft_split(char const *str, char ch);
char *ft_itoa(int n); char *ft_itoa(int n);
char *ft_itox(unsigned long n, const char *base);
char *ft_utoa(unsigned int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
void ft_striteri(char *s, void (*f)(unsigned int, char*)); void ft_striteri(char *s, void (*f)(unsigned int, char*));
void ft_putchar_fd(char c, int fd); void ft_putchar_fd(char c, int fd);

Loading…
Cancel
Save