adding labs
This commit is contained in:
1
Makefile
1
Makefile
@@ -122,6 +122,7 @@ SRCS = ft_memset.c \
|
||||
pf_utils.c \
|
||||
\
|
||||
ft_abs.c \
|
||||
ft_labs.c \
|
||||
ft_fabs.c \
|
||||
ft_greater.c \
|
||||
ft_smaller.c \
|
||||
|
||||
@@ -61,7 +61,7 @@ size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n
|
||||
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_itoa_static(long int nbr, char *buffer, size_t nbr_len);
|
||||
char *ft_itoa_static(long int nbr, char *buffer, size_t buff_len);
|
||||
char *ft_utoa(unsigned long int n);
|
||||
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
|
||||
|
||||
@@ -124,6 +124,7 @@ void ft_putnbrendl(int n);
|
||||
void ft_putnbrendl_fd(int n, int fd);
|
||||
char *ft_concat_free(char *str1, char *str2);
|
||||
int ft_abs(int n);
|
||||
long int ft_labs(long int n);
|
||||
double ft_fabs(double n);
|
||||
int ft_greater(int a, int b);
|
||||
int ft_smaller(int a, int b);
|
||||
|
||||
@@ -55,21 +55,19 @@ char *ft_itoa_static(long int nbr, char *buffer, size_t buff_len)
|
||||
}
|
||||
|
||||
// Determine if the number is negative
|
||||
is_negative = ft_sign_f(nbr);
|
||||
if (is_negative)
|
||||
{
|
||||
nbr = -nbr; // Make nbr positive for processing
|
||||
}
|
||||
is_negative = nbr < 0;
|
||||
nbr = ft_labs(nbr);
|
||||
|
||||
// Write the null terminator at the end
|
||||
buffer[buff_len] = '\0';
|
||||
buffer[buff_len - 1] = '\0';
|
||||
|
||||
// Write the digits in reverse order
|
||||
i = buff_len - 1;
|
||||
i = buff_len - 2;
|
||||
do
|
||||
{
|
||||
buffer[i--] = (nbr % 10) + '0';
|
||||
buffer[i] = (nbr % 10) + '0';
|
||||
nbr /= 10;
|
||||
i--;
|
||||
} while (nbr != 0);
|
||||
|
||||
// Add the negative sign if needed
|
||||
|
||||
8
srcs/ft_labs.c
Normal file
8
srcs/ft_labs.c
Normal file
@@ -0,0 +1,8 @@
|
||||
#include "libft.h"
|
||||
|
||||
long int ft_labs(long int n)
|
||||
{
|
||||
if (n < 0)
|
||||
n *= -1;
|
||||
return (n);
|
||||
}
|
||||
@@ -7,10 +7,12 @@
|
||||
size_t ft_nbrlen(int nbr)
|
||||
{
|
||||
int len;
|
||||
int is_negative;
|
||||
|
||||
is_negative = nbr < 0;
|
||||
len = (nbr < 0) ? 2 : 1;
|
||||
while (nbr /= 10)
|
||||
len++;
|
||||
|
||||
return len;
|
||||
return len + is_negative;
|
||||
}
|
||||
@@ -15,12 +15,19 @@
|
||||
|
||||
size_t ft_strjoin_static(char *dst, size_t dst_size, const char **srcs, size_t n)
|
||||
{
|
||||
size_t total_len;
|
||||
size_t i;
|
||||
|
||||
// Calculate total length needed
|
||||
size_t total_len = 0;
|
||||
for (size_t i = 0; i < n; i++)
|
||||
total_len = 0;
|
||||
i = 0;
|
||||
while (i < n)
|
||||
{
|
||||
total_len += ft_strlen(srcs[i]);
|
||||
ft_printf("--ft_strlen(srcs[%i]):%i--", i, ft_strlen(srcs[i]));
|
||||
i++;
|
||||
}
|
||||
ft_printf("{dst_size:%li,srcs[0]:%s,srcs[1]:%s,total_len:%i}", dst_size, srcs[0], srcs[1], total_len); // debug
|
||||
|
||||
// Check if buffer is large enough (include space for '\0')
|
||||
if (total_len + 1 > dst_size)
|
||||
|
||||
@@ -11,7 +11,7 @@
|
||||
/* ************************************************************************** */
|
||||
|
||||
/*
|
||||
** return length of of string
|
||||
** return length of of string, excluding the terminal '\0'
|
||||
*/
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
Reference in New Issue
Block a user