summaryrefslogtreecommitdiff
path: root/ft_substr.c
diff options
context:
space:
mode:
Diffstat (limited to 'ft_substr.c')
-rw-r--r--ft_substr.c47
1 files changed, 47 insertions, 0 deletions
diff --git a/ft_substr.c b/ft_substr.c
new file mode 100644
index 0000000..f5ee2aa
--- /dev/null
+++ b/ft_substr.c
@@ -0,0 +1,47 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_substr.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* 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 */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+
+char *ft_substr(char const *s, unsigned int start, size_t len)
+{
+ size_t i;
+ char *result;
+
+ i = 0;
+ while (s[i])
+ i++;
+ if (i - start < len)
+ len = (i - start);
+ if (start >= len)
+ len = 0;
+ result = malloc(len + 1);
+ if (result)
+ {
+ result[len] = '\0';
+ i = 0;
+ while (i < len)
+ {
+ result[i] = s[i + start];
+ i++;
+ }
+ }
+ return (result);
+}
+
+/* #include <stdio.h> */
+/* int main () */
+/* { */
+/* char s[] = "Hello there"; */
+/* char *substr = ft_substr(s, 25, 10); */
+/* printf("%s\n", substr); */
+/* } */