From 28be9d6b91d5313fe36f3c8f9c82ce13adeef87a Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 8 Sep 2025 10:21:48 +0200 Subject: [PATCH] feat(convert/atou): adding the atou function - Can be usefull when the parsing of some project (like philosophers) --- convert/ft_atou.c | 40 ++++++++++++++++++++++++++++++++++++++++ includes/convert.h | 12 +++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 convert/ft_atou.c diff --git a/convert/ft_atou.c b/convert/ft_atou.c new file mode 100644 index 0000000..5385e94 --- /dev/null +++ b/convert/ft_atou.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_atou.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rparodi +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/11/08 17:22:41 by rparodi #+# #+# */ +/* Updated: 2025/09/08 10:16:01 by rparodi ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include + +static int ft_check_space(int c) +{ + if (c == 32 || (c >= 9 && c <= 13)) + return (1); + return (0); +} + +size_t ft_atou(const char *nptr) +{ + size_t i; + size_t number; + + if (!nptr) + return (0); + i = 0; + number = 0; + while (nptr[i]) + { + if (nptr[i] >= '0' && nptr[i] <= '9') + number = (number * 10) + nptr[i] - '0'; + else + break ; + i++; + } + return (number); +} diff --git a/includes/convert.h b/includes/convert.h index f3dcd96..d7867c3 100644 --- a/includes/convert.h +++ b/includes/convert.h @@ -6,13 +6,15 @@ /* By: rparodi +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/31 14:57:24 by rparodi #+# #+# */ -/* Updated: 2025/09/05 16:07:54 by rparodi ### ########.fr */ +/* Updated: 2025/09/08 10:17:01 by rparodi ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef CONVERT_H # define CONVERT_H +# include + /** * @brief Converts integer to string * @@ -29,6 +31,14 @@ char *ft_itoa(int n); */ int ft_atoi(const char *nptr); +/** + * @brief Converts string to size_t + * + * @param nptr the string that will be converted + * @return The unsigned long long on the string + */ +size_t ft_atou(const char *nptr); + /** * @brief Converts string to long long integer *