From: Lukas Jiriste Date: Sat, 15 Jun 2024 07:37:30 +0000 (+0200) Subject: Implement ft_remove_space function X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=844fb52cc57fdd62d5835c910f4dcd218d7abcb3;p=Libft.git Implement ft_remove_space function --- diff --git a/Makefile b/Makefile index 620c5e2..1895cf6 100644 --- 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 index 0000000..931cfa5 --- /dev/null +++ b/ft_str/ft_remove_space.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_remove_space.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 + +// 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); +} diff --git a/inc/ft_str.h b/inc/ft_str.h index 409d490..6adc40d 100644 --- a/inc/ft_str.h +++ b/inc/ft_str.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 *));