From d8ec79480f639d52389d155aa83814a7c72ed4e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 17 Oct 2024 12:11:30 +0200 Subject: [PATCH] ft_strjoin: initial implementation --- Makefile | 5 +++-- ft_strjoin.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ libft.h | 1 + 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 ft_strjoin.c diff --git a/Makefile b/Makefile index b999cb0..9e81401 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/17 11:44:01 by kcolin ### ########.fr # +# Updated: 2024/10/17 12:03:18 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -35,7 +35,8 @@ SOURCES = ft_isalpha.c \ ft_atoi.c \ ft_calloc.c \ ft_strdup.c \ - ft_substr.c + ft_substr.c \ + ft_strjoin.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_strjoin.c b/ft_strjoin.c new file mode 100644 index 0000000..f479d42 --- /dev/null +++ b/ft_strjoin.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strjoin.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 12:03:29 by kcolin #+# #+# */ +/* Updated: 2024/10/17 12:09:03 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strjoin(char const *s1, char const *s2) +{ + char *out; + size_t len; + + len = ft_strlen(s1) + ft_strlen(s2) + 1; + out = malloc(len); + if (out == NULL) + return (NULL); + ft_strlcpy(out, s1, len); + ft_strlcat(out, s2, len); + return (out); +} + +/* +#include // BAD + +int main(int argc, char **argv) +{ + if (argc > 2) + { + char *str; + + str = ft_strjoin(argv[1], argv[2]); + printf("'%s'\n", str); + free(str); + } + return (0); +} +*/ diff --git a/libft.h b/libft.h index 5a54990..bdc8468 100644 --- a/libft.h +++ b/libft.h @@ -48,5 +48,6 @@ void *ft_calloc(size_t nmemb, size_t size); char *ft_strdup(const char *s); char *ft_substr(char const *s, unsigned int start, size_t len); +char *ft_strjoin(char const *s1, char const *s2); #endif