]> git.dkaiser.de - 42/libft.git/commitdiff
Add everything
authorDominik Kaiser <dkaiser@1-C-13.42heilbronn.de>
Sun, 10 Mar 2024 12:35:18 +0000 (13:35 +0100)
committerDominik Kaiser <dkaiser@1-C-13.42heilbronn.de>
Sun, 10 Mar 2024 12:35:18 +0000 (13:35 +0100)
ft_atoi.o [new file with mode: 0644]
ft_isalpha.o [new file with mode: 0644]
ft_itoa.c [new file with mode: 0644]
ft_putnbr_fd.c [new file with mode: 0644]
ft_split.c [new file with mode: 0644]
ft_striteri.c [new file with mode: 0644]
ft_strjoin.c [new file with mode: 0644]
ft_strmapi.c [new file with mode: 0644]
ft_strtrim.c [new file with mode: 0644]
ft_substr.c [new file with mode: 0644]

diff --git a/ft_atoi.o b/ft_atoi.o
new file mode 100644 (file)
index 0000000..8280d24
Binary files /dev/null and b/ft_atoi.o differ
diff --git a/ft_isalpha.o b/ft_isalpha.o
new file mode 100644 (file)
index 0000000..b75a8da
Binary files /dev/null and b/ft_isalpha.o differ
diff --git a/ft_itoa.c b/ft_itoa.c
new file mode 100644 (file)
index 0000000..401ba6a
--- /dev/null
+++ b/ft_itoa.c
@@ -0,0 +1,68 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_itoa.c                                          :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 10:42:08 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:18:58 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static int     get_last_digit(int n)
+{
+       int     last_digit;
+
+       last_digit = n % 10;
+       if (last_digit < 0)
+               last_digit *= -1;
+       return (last_digit);
+}
+
+static int     get_len(int n)
+{
+       int     len;
+
+       len = 0;
+       if (n == 0)
+               return (1);
+       if (n < 0)
+               len++;
+       while (n)
+       {
+               len++;
+               n /= 10;
+       }
+       return (len);
+}
+
+char   *ft_itoa(int n)
+{
+       char    *result;
+       int             len;
+
+       len = get_len(n);
+       result = malloc(sizeof(char) * (len + 1));
+       if (!result)
+               return (0);
+       result[len] = '\0';
+       if (n == 0)
+               result[0] = '0';
+       if (n < 0)
+               result[0] = '-';
+       while (n)
+       {
+               result[--len] = '0' + get_last_digit(n);
+               n /= 10;
+       }
+       return (result);
+}
+
+/* #include <stdio.h> */
+/* int main() { */
+/*     char* itoa = ft_itoa(0); */
+/*     printf("%s\n", itoa); */
+/* } */
diff --git a/ft_putnbr_fd.c b/ft_putnbr_fd.c
new file mode 100644 (file)
index 0000000..96f5f4b
--- /dev/null
@@ -0,0 +1,38 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_putnbr_fd.c                                     :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 10:43:09 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:20:59 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static void    putnbr_fd_rec(int n, int fd)
+{
+       char    c;
+
+       c = '0' + n % 10;
+       if (n > 9)
+               putnbr_fd_rec(n / 10, fd);
+       write(fd, &c, 1);
+}
+
+void   ft_putnbr_fd(int n, int fd)
+{
+       if (n == -2147483648)
+       {
+               write(fd, "-2147483648", 11);
+               return ;
+       }
+       if (n < 0)
+       {
+               write(fd, "-", 1);
+               n *= -1;
+       }
+       putnbr_fd_rec(n, fd);
+}
diff --git a/ft_split.c b/ft_split.c
new file mode 100644 (file)
index 0000000..e9ba9f9
--- /dev/null
@@ -0,0 +1,104 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_split.c                                         :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/08 15:36:44 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:09:06 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+static int     get_word_count(char const *s, char c)
+{
+       int     word_count;
+       int     cur_len;
+
+       word_count = 0;
+       cur_len = 0;
+       while (*s)
+       {
+               if (!(*s) || *s == c)
+               {
+                       cur_len = 0;
+               }
+               else
+               {
+                       if (!cur_len)
+                               word_count++;
+                       cur_len++;
+               }
+               s++;
+       }
+       return (word_count);
+}
+
+static char    *get_next_token(char const **ptr_s, char c)
+{
+       int                     i;
+       int                     len;
+       char const      *s;
+       char            *result;
+
+       s = *ptr_s;
+       while (*s && *s == c)
+               s++;
+       if (!*s)
+               return (0);
+       len = 0;
+       while (s[len] && s[len] != c)
+               len++;
+       result = malloc(sizeof(char) * (len + 1));
+       if (!result)
+               return (0);
+       i = 0;
+       while (i < len)
+               result[i++] = *(s++);
+       result[i] = '\0';
+       *ptr_s = s;
+       return (result);
+}
+
+char   **ft_split(char const *s, char c)
+{
+       char    **result;
+       int             word_count;
+       int             w;
+
+       word_count = get_word_count((char *)s, c);
+       result = malloc(sizeof(char *) * (word_count + 1));
+       if (!result)
+               return (0);
+       w = 0;
+       while (w < word_count)
+       {
+               result[w] = get_next_token(&s, c);
+               if (!result[w])
+               {
+                       while (w--)
+                               free(result[w]);
+                       free(result);
+                       return (0);
+               }
+               w++;
+       }
+       result[w] = 0;
+       return (result);
+}
+
+/* #include <stdio.h> */
+/* int main() */
+/* { */
+/*     char s[] = "      split       this for   me  !"; */
+/*     char **split = ft_split(s, ' '); */
+
+/*     if (split) { */
+/*         while(*split) { */
+/*             printf("%s\n", *split); */
+/*             split++; */
+/*         } */
+/*     } */
+/* } */
diff --git a/ft_striteri.c b/ft_striteri.c
new file mode 100644 (file)
index 0000000..e7c8b89
--- /dev/null
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_striteri.c                                      :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 11:24:32 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:19:39 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+void   ft_striteri(char *s, void (*f)(unsigned int, char *))
+{
+       unsigned int    i;
+
+       i = 0;
+       while (s[i])
+       {
+               f(i, &s[i]);
+               i++;
+       }
+}
diff --git a/ft_strjoin.c b/ft_strjoin.c
new file mode 100644 (file)
index 0000000..c399c87
--- /dev/null
@@ -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)); */
+/* } */
diff --git a/ft_strmapi.c b/ft_strmapi.c
new file mode 100644 (file)
index 0000000..5d31408
--- /dev/null
@@ -0,0 +1,46 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strmapi.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/10 11:11:30 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/10 13:19:18 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "libft.h"
+
+char   *ft_strmapi(char const *s, char (*f)(unsigned int, char))
+{
+       char                    *result;
+       unsigned int    i;
+
+       i = 0;
+       while (s[i])
+               i++;
+       result = malloc(sizeof(char) * (i + 1));
+       if (!result)
+               return (0);
+       result[i] = '\0';
+       i = 0;
+       while (s[i])
+       {
+               result[i] = f(i, s[i]);
+               i++;
+       }
+       return (result);
+}
+
+/* char func (unsigned int i, char c) */
+/* { */
+/*     c += i; */
+/*     return (c); */
+/* } */
+
+/* #include <stdio.h> */
+/* int main(){ */
+/*     char str[] = "AAAAAAAA"; */
+/*     printf("%s\n", ft_strmapi(str, func)); */
+/* } */
diff --git a/ft_strtrim.c b/ft_strtrim.c
new file mode 100644 (file)
index 0000000..933987b
--- /dev/null
@@ -0,0 +1,63 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_strtrim.c                                       :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: dkaiser <dkaiser@student.42heilbronn.de    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/03/07 10:24:17 by dkaiser           #+#    #+#             */
+/*   Updated: 2024/03/07 14:47:40 by dkaiser          ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include <stdlib.h>
+
+static int     is_in(char c, const char *str)
+{
+       while (*str)
+       {
+               if (c == *str)
+                       return (1);
+               else
+                       str++;
+       }
+       return (0);
+}
+
+char   *ft_strtrim(char const *s1, char const *set)
+{
+       int             i;
+       char    *start;
+       char    *end;
+       char    *result;
+
+       start = (char *)s1;
+       while (is_in(*start, set))
+               start++;
+       i = 0;
+       while (s1[i])
+               i++;
+       end = (char *)s1 + i - 1;
+       while (end > start && is_in(*end, set))
+               end--;
+       i = end - start + 1;
+       result = malloc(i + 1);
+       if (result)
+       {
+               result[i] = '\0';
+               i = 0;
+               while (start <= end)
+                       result[i++] = *(start++);
+               return (result);
+       }
+       return (0);
+}
+
+/* #include <stdio.h> */
+
+/* int main(void) */
+/* { */
+/*     char    s1[] = " \t   \t  \t  "; */
+
+/*     printf("%s\n", ft_strtrim(s1, " \t")); */
+/* } */
diff --git a/ft_substr.c b/ft_substr.c
new file mode 100644 (file)
index 0000000..f5ee2aa
--- /dev/null
@@ -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); */
+/* } */