Add ft_isint function
authorLukas Jiriste <ljiriste@student.42prague.com>
Wed, 10 Apr 2024 11:20:46 +0000 (13:20 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Wed, 10 Apr 2024 11:20:46 +0000 (13:20 +0200)
Makefile
ft_check/ft_isint.c [new file with mode: 0644]
inc/ft_check.h

index 431f293dd015ca0e8320d0e1bdd25f81836c0b8e..0bc20e16a1159278686e3014cfc844a633c3d483 100644 (file)
--- 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 (file)
index 0000000..acffdee
--- /dev/null
@@ -0,0 +1,45 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_isint.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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 <stdlib.h>
+
+//     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);
+}
index 4c049dc2e13b53bc898808a47dccbc18cbf11026..e640d931216cf4f71bcedb47680c505ed3f7a174 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   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