Adding the libft
This commit is contained in:
parent
1b98b537b7
commit
c947e43e51
62 changed files with 2098 additions and 19 deletions
60
libft/convert/ft_atoi.c
Normal file
60
libft/convert/ft_atoi.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoi.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/08 17:22:41 by rparodi #+# #+# */
|
||||
/* Updated: 2024/10/31 15:16:10 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int ft_check_space(int c)
|
||||
{
|
||||
if (c == 32 || (c >= 9 && c <= 13))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_check_sign(const char *nptr, int *i)
|
||||
{
|
||||
while (ft_check_space(nptr[*i]))
|
||||
(*i)++;
|
||||
if (nptr[*i] == '-')
|
||||
{
|
||||
(*i)++;
|
||||
return (-1);
|
||||
}
|
||||
else if (nptr[*i] == '+')
|
||||
(*i)++;
|
||||
return (1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts string to integer
|
||||
*
|
||||
* @param nptr the string that will be converted
|
||||
* @return The integer on the string
|
||||
*/
|
||||
int ft_atoi(const char *nptr)
|
||||
{
|
||||
int i;
|
||||
int sign;
|
||||
int number;
|
||||
|
||||
i = 0;
|
||||
sign = ft_check_sign(nptr, &i);
|
||||
number = 0;
|
||||
while (nptr[i])
|
||||
{
|
||||
if (nptr[i] >= '0' && nptr[i] <= '9')
|
||||
number = (number * 10) + nptr[i] - '0';
|
||||
else
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
return (sign * number);
|
||||
}
|
||||
60
libft/convert/ft_atoll.c
Normal file
60
libft/convert/ft_atoll.c
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_atoll.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <rparodi@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/10/31 15:12:07 by rparodi #+# #+# */
|
||||
/* Updated: 2024/10/31 15:14:50 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static int ft_check_space(int c)
|
||||
{
|
||||
if (c == 32 || (c >= 9 && c <= 13))
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int ft_check_sign(const char *nptr, size_t *i)
|
||||
{
|
||||
while (ft_check_space(nptr[*i]))
|
||||
(*i)++;
|
||||
if (nptr[*i] == '-')
|
||||
{
|
||||
(*i)++;
|
||||
return (-1);
|
||||
}
|
||||
else if (nptr[*i] == '+')
|
||||
(*i)++;
|
||||
return (1);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts string to long long integer
|
||||
*
|
||||
* @param nptr the string that will be converted
|
||||
* @return The long long int on the string
|
||||
*/
|
||||
long long int ft_atoll(const char *nptr)
|
||||
{
|
||||
size_t i;
|
||||
int sign;
|
||||
long long int number;
|
||||
|
||||
i = 0;
|
||||
sign = ft_check_sign(nptr, &i);
|
||||
number = 0;
|
||||
while (nptr[i])
|
||||
{
|
||||
if (nptr[i] >= '0' && nptr[i] <= '9')
|
||||
number = (number * 10) + nptr[i] - '0';
|
||||
else
|
||||
break ;
|
||||
i++;
|
||||
}
|
||||
return (sign * number);
|
||||
}
|
||||
58
libft/convert/ft_itoa.c
Normal file
58
libft/convert/ft_itoa.c
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_itoa.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: rparodi <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/11/09 13:56:30 by rparodi #+# #+# */
|
||||
/* Updated: 2024/10/31 13:02:55 by rparodi ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "libft.h"
|
||||
|
||||
static size_t ft_check_sign(char *str, long *nb)
|
||||
{
|
||||
if ((*nb) == 0)
|
||||
str[0] = '0';
|
||||
if ((*nb) < 0)
|
||||
{
|
||||
str[0] = '-';
|
||||
*nb = -*nb;
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Converts integer to string
|
||||
*
|
||||
* @param n the integer that will be converted
|
||||
* @return The string with this integer
|
||||
*/
|
||||
char *ft_itoa(int n)
|
||||
{
|
||||
size_t i;
|
||||
size_t modulus;
|
||||
short int maiboyerlpb;
|
||||
char str[13];
|
||||
long nb;
|
||||
|
||||
nb = n;
|
||||
maiboyerlpb = 0;
|
||||
modulus = 1000000000;
|
||||
ft_bzero(str, 13);
|
||||
i = ft_check_sign(str, &nb);
|
||||
while (modulus != 0)
|
||||
{
|
||||
if (nb / modulus != 0 || maiboyerlpb != 0)
|
||||
{
|
||||
str[i++] = (nb / modulus) + 48;
|
||||
maiboyerlpb++;
|
||||
}
|
||||
nb %= modulus;
|
||||
modulus /= 10;
|
||||
}
|
||||
return (ft_strdup(str));
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue