From 7d896b939c6215a739e95a253f34c168ca394f14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Thu, 17 Oct 2024 12:00:30 +0200 Subject: [PATCH] ft_substr: initial implementation --- Makefile | 5 +++-- ft_substr.c | 39 +++++++++++++++++++++++++++++++++++++++ libft.h | 3 ++- 3 files changed, 44 insertions(+), 3 deletions(-) create mode 100644 ft_substr.c diff --git a/Makefile b/Makefile index 306c0bb..b999cb0 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/17 10:52:23 by kcolin ### ########.fr # +# Updated: 2024/10/17 11:44:01 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -34,7 +34,8 @@ SOURCES = ft_isalpha.c \ ft_strnstr.c \ ft_atoi.c \ ft_calloc.c \ - ft_strdup.c + ft_strdup.c \ + ft_substr.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_substr.c b/ft_substr.c new file mode 100644 index 0000000..cce6997 --- /dev/null +++ b/ft_substr.c @@ -0,0 +1,39 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_substr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 11:45:52 by kcolin #+# #+# */ +/* Updated: 2024/10/17 11:58:18 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_substr(char const *s, unsigned int start, size_t len) +{ + char *out; + + out = malloc(sizeof(char) * len + 1); + if (out == NULL) + return (NULL); + ft_strlcpy(out, s + start, len + 1); + return (out); +} + +/* +#include // BAD + +int main(void) +{ + char *data = "This is a long test string."; + + printf("'%s'\n", ft_substr(data, 0, 4)); + printf("'%s'\n", ft_substr(data, 15, 4)); + printf("'%s'\n", ft_substr(data, 15, 0)); + printf("'%s'\n", ft_substr(data, 20, 20)); +} +*/ diff --git a/libft.h b/libft.h index b5c3fff..5a54990 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */ -/* Updated: 2024/10/17 11:03:30 by kcolin ### ########.fr */ +/* Updated: 2024/10/17 11:40:51 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -47,5 +47,6 @@ int ft_atoi(const char *nptr); 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); #endif