From: Lukas Jiriste Date: Wed, 10 Apr 2024 11:20:46 +0000 (+0200) Subject: Add ft_isint function X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=f9d636c2f08b1d32ff333d50c7715b79897f09da;p=Libft.git Add ft_isint function --- diff --git a/Makefile b/Makefile index 431f293..0bc20e1 100644 --- a/Makefile +++ b/Makefile @@ -58,6 +58,7 @@ SRCcheck:= ft_isalnum.c \ ft_islower.c \ ft_isupper.c \ ft_isprint.c \ + ft_isint.c \ SRCconv := ft_itoa_base.c \ ft_tolower.c \ diff --git a/ft_check/ft_isint.c b/ft_check/ft_isint.c new file mode 100644 index 0000000..acffdee --- /dev/null +++ b/ft_check/ft_isint.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isint.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/04/10 12:44:56 by ljiriste #+# #+# */ +/* Updated: 2024/04/10 13:17:33 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_check.h" +#include "libft.h" +#include + +// Checks whether str is a "pure" int +// that is whether the string only contains digits +// and whether it fits inside int +int ft_isint(const char *str) +{ + const int num = ft_atoi(str); + char *newstr; + int sign; + int res; + + sign = 1; + newstr = ft_itoa(num); + while (ft_isspace(*str)) + ++str; + while (*str == '+' || *str == '-') + { + if (*str == '-') + sign *= -1; + ++str; + } + while (*str == '0') + ++str; + if (sign == -1) + res = !ft_strcmp(str, newstr + 1); + else + res = !ft_strcmp(str, newstr); + free(newstr); + return (res); +} diff --git a/inc/ft_check.h b/inc/ft_check.h index 4c049dc..e640d93 100644 --- a/inc/ft_check.h +++ b/inc/ft_check.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/09 11:55:15 by ljiriste #+# #+# */ -/* Updated: 2023/12/09 15:13:41 by ljiriste ### ########.fr */ +/* Updated: 2024/04/10 12:54:40 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,5 +21,6 @@ int ft_isprint(int c); int ft_isspace(int c); int ft_isupper(int c); int ft_isascii(int c); +int ft_isint(const char *str); #endif