summaryrefslogtreecommitdiff
path: root/ft_substr.c
diff options
context:
space:
mode:
authorDominik Kaiser2024-03-10 13:37:45 +0100
committerDominik Kaiser2024-03-10 13:37:45 +0100
commit4e19b82398e0cfaa3ca0b9f68772c88d103ff6ea (patch)
treee25f459959a69578322529aca6d2a02cb2d3dfcf /ft_substr.c
parent3c90d29fe5bf9ba6da090cd59c8c139d269f8bd4 (diff)
downloadlibft-4e19b82398e0cfaa3ca0b9f68772c88d103ff6ea.tar.gz
libft-4e19b82398e0cfaa3ca0b9f68772c88d103ff6ea.zip
Complete missing functions and add header
Diffstat (limited to 'ft_substr.c')
-rw-r--r--ft_substr.c25
1 files changed, 12 insertions, 13 deletions
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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/06 21:58:31 by dkaiser #+# #+# */
-/* Updated: 2024/03/07 14:55:55 by dkaiser ### ########.fr */
+/* Updated: 2024/03/10 13:18:11 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
-#include <stdlib.h>
+#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); */
/* } */