summaryrefslogtreecommitdiff
path: root/ft_strjoin.c
diff options
context:
space:
mode:
Diffstat (limited to 'ft_strjoin.c')
-rw-r--r--ft_strjoin.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/ft_strjoin.c b/ft_strjoin.c
new file mode 100644
index 0000000..c399c87
--- /dev/null
+++ b/ft_strjoin.c
@@ -0,0 +1,64 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_strjoin.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/03/07 10:15:33 by dkaiser #+# #+# */
+/* Updated: 2024/03/07 14:11:30 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+
+static int get_len(char const *s)
+{
+ int i;
+
+ i = 0;
+ while (s[i])
+ i++;
+ return (i);
+}
+
+static int copy_str(char *dst, const char *src)
+{
+ int i;
+
+ i = 0;
+ while (src[i])
+ {
+ dst[i] = src[i];
+ i++;
+ }
+ return (i);
+}
+
+char *ft_strjoin(char const *s1, char const *s2)
+{
+ int len;
+ char *result;
+
+ len = get_len(s1) + get_len(s2);
+ result = malloc(len + 1);
+ if (result)
+ {
+ result[len] = '\0';
+ len = copy_str(result, s1);
+ len = copy_str(result + len, s2);
+ return (result);
+ }
+ else
+ return (0);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/* char s1[] = "Hello "; */
+/* char s2[] = "World"; */
+
+/* printf("%s\n", ft_strjoin(s1, s2)); */
+/* } */