From 4e19b82398e0cfaa3ca0b9f68772c88d103ff6ea Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sun, 10 Mar 2024 13:37:45 +0100 Subject: Complete missing functions and add header --- ft_substr.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'ft_substr.c') diff --git a/ft_substr.c b/ft_substr.c index f5ee2aa..bf65c8d 100644 --- a/ft_substr.c +++ b/ft_substr.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" char *ft_substr(char const *s, unsigned int start, size_t len) { @@ -20,20 +20,19 @@ char *ft_substr(char const *s, unsigned int start, size_t len) i = 0; while (s[i]) i++; + if (start >= i) + len = 0; if (i - start < len) len = (i - start); - if (start >= len) - len = 0; result = malloc(len + 1); - if (result) + if (!result) + return (0); + result[len] = '\0'; + i = 0; + while (i < len) { - result[len] = '\0'; - i = 0; - while (i < len) - { - result[i] = s[i + start]; - i++; - } + result[i] = s[i + start]; + i++; } return (result); } @@ -42,6 +41,6 @@ char *ft_substr(char const *s, unsigned int start, size_t len) /* int main () */ /* { */ /* char s[] = "Hello there"; */ -/* char *substr = ft_substr(s, 25, 10); */ +/* char *substr = ft_substr(s, 0, 2); */ /* printf("%s\n", substr); */ /* } */ -- cgit v1.2.3