Implement ft_remove_space function
authorLukas Jiriste <ljiriste@student.42prague.com>
Sat, 15 Jun 2024 07:37:30 +0000 (09:37 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sat, 15 Jun 2024 07:37:30 +0000 (09:37 +0200)
Makefile
ft_str/ft_remove_space.c [new file with mode: 0644]
inc/ft_str.h

index 620c5e2beecacbdc623f987e8f22ef1bef4fb6b9..1895cf6c135dabc2bc414e0d8b3730e391ee669f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -32,6 +32,7 @@ SRCstr        :=      ft_strcat_alloc.c               \
                        ft_striteri.c                   \
                        ft_strmapi.c                    \
                        ft_strjoin.c                    \
+                       ft_remove_space.c               \
                        ft_split.c                              \
                        ft_substr.c                             \
                        ft_strlcat.c                    \
diff --git a/ft_str/ft_remove_space.c b/ft_str/ft_remove_space.c
new file mode 100644 (file)
index 0000000..931cfa5
--- /dev/null
@@ -0,0 +1,37 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_remove_space.c                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/06/15 08:27:22 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/06/15 08:36:48 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_str.h"
+#include "libft.h"
+#include <stdlib.h>
+
+//     This function could be improved not to overallocate
+char   *ft_remove_space(const char *str)
+{
+       char    *res;
+       size_t  i;
+       size_t  j;
+
+       res = malloc(ft_strlen(str) + 1);
+       if (!res)
+               return (NULL);
+       i = 0;
+       j = 0;
+       while (str[i])
+       {
+               if (!ft_isspace(str[i]))
+                       res[j++] = str[i];
+               ++i;
+       }
+       res[j] = '\0';
+       return (res);
+}
index 409d49001f5d55ced80f59a5f50fd73702b84004..6adc40dd13d98246695228a8f754ad0376e87637 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/12/09 11:50:15 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/05/02 11:23:35 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/06/15 08:34:22 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -30,6 +30,7 @@ char  *ft_strndup(const char *s, size_t n);
 char   *ft_substr(const char *s, unsigned int start, size_t len);
 char   *ft_strjoin(const char *s1, const char *s2);
 char   *ft_strtrim(const char *s1, const char *set);
+char   *ft_remove_space(const char *str);
 char   **ft_split(const char *s, char c);
 char   *ft_strmapi(const char *s, char (*f)(unsigned int, char));
 void   ft_striteri(char *s, void (*f)(unsigned int, char *));