From 2cc6f50e5b04b903ca97d8a5479c95ca72b52547 Mon Sep 17 00:00:00 2001 From: JozanLeClerc Date: Sun, 26 Apr 2020 20:07:03 +0200 Subject: substsubtsubst --- libft/src/ft_strsubst.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 libft/src/ft_strsubst.c (limited to 'libft/src/ft_strsubst.c') diff --git a/libft/src/ft_strsubst.c b/libft/src/ft_strsubst.c new file mode 100644 index 0000000..dd89917 --- /dev/null +++ b/libft/src/ft_strsubst.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strsubst.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: rbousset +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */ +/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */ +/* */ +/* ************************************************************************** */ + +#include +#include + +char + *ft_strsubst(char *str, const char *pattern, const char *subst) +{ + size_t nlen; + char *nstr; + char *ptr; + + if (!(ptr = ft_strnstr(str, pattern, ft_strlen(str)))) + return (NULL); + nlen = ft_strlen(str) - ft_strlen(pattern) + ft_strlen(subst); + if (!(nstr = (char*)malloc((nlen + 1) * sizeof(char)))) + return (NULL); + ft_memcpy(nstr, str, ptr - str); + ft_memcpy(nstr + (ptr - str), subst, ft_strlen(subst)); + ft_memcpy(nstr + (ptr - str + ft_strlen(subst)), + str + (ptr - str + ft_strlen(pattern)), + ft_strlen(str + (ptr - str + ft_strlen(pattern)))); + nstr[nlen + 1] = '\0'; + ft_memdel((void*)&str); + return (nstr); +} -- cgit v1.2.3