From 4e19b82398e0cfaa3ca0b9f68772c88d103ff6ea Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sun, 10 Mar 2024 13:37:45 +0100 Subject: [PATCH] Complete missing functions and add header --- Makefile | 36 +++++++++++++++++++++++++++++++++++- ft_atoi.c | 4 +++- ft_bzero.c | 4 ++-- ft_calloc.c | 4 ++-- ft_isalnum.c | 4 +++- ft_isalpha.c | 4 +++- ft_isdigit.c | 4 +++- ft_isprint.c | 4 +++- ft_memchr.c | 4 ++-- ft_memcmp.c | 4 ++-- ft_memcpy.c | 4 ++-- ft_memmove.c | 4 ++-- ft_memset.c | 4 ++-- ft_putchar_fd.c | 4 ++-- ft_putendl_fd.c | 4 ++-- ft_putstr_fd.c | 4 ++-- ft_strchr.c | 17 +++++++++++------ ft_strdup.c | 19 ++++++++++++------- ft_strjoin.c | 4 ++-- ft_strlcat.c | 4 ++-- ft_strlcpy.c | 4 ++-- ft_strlen.c | 4 +++- ft_strncmp.c | 4 ++-- ft_strnstr.c | 17 ++++++++++------- ft_strrchr.c | 4 ++-- ft_strtrim.c | 4 ++-- ft_substr.c | 25 ++++++++++++------------- ft_tolower.c | 4 +++- ft_toupper.c | 4 +++- libft.h | 10 +++++++--- 30 files changed, 143 insertions(+), 77 deletions(-) diff --git a/Makefile b/Makefile index 3920b81..8759efa 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,41 @@ NAME=libft.a CC=cc CFLAGS=-Wall -Wextra -Werror -SRC_FILES = $(wildcard *.c) +SRC_FILES = ft_atoi.c \ + ft_bzero.c \ + ft_calloc.c \ + ft_isalnum.c \ + ft_isalpha.c \ + ft_isascii.c \ + ft_isdigit.c \ + ft_isprint.c \ + ft_itoa.c \ + ft_memchr.c \ + ft_memcmp.c \ + ft_memcpy.c \ + ft_memmove.c \ + ft_memset.c \ + ft_putchar_fd.c \ + ft_putendl_fd.c \ + ft_putnbr_fd.c \ + ft_putstr_fd.c \ + ft_split.c \ + ft_strchr.c \ + ft_strdup.c \ + ft_striteri.c \ + ft_strjoin.c \ + ft_strlcat.c \ + ft_strlcpy.c \ + ft_strlen.c \ + ft_strmapi.c \ + ft_strncmp.c \ + ft_strnstr.c \ + ft_strrchr.c \ + ft_strtrim.c \ + ft_substr.c \ + ft_tolower.c \ + ft_toupper.c \ + OBJ_FILES = $(SRC_FILES:.c=.o) diff --git a/ft_atoi.c b/ft_atoi.c index 568a616..90200fb 100644 --- a/ft_atoi.c +++ b/ft_atoi.c @@ -6,10 +6,12 @@ /* By: dkaiser +#include "libft.h" void ft_bzero(void *s, size_t n) { diff --git a/ft_calloc.c b/ft_calloc.c index 5baf8f1..2b711d9 100644 --- a/ft_calloc.c +++ b/ft_calloc.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" void *ft_calloc(size_t count, size_t size) { diff --git a/ft_isalnum.c b/ft_isalnum.c index 4de4a63..d705500 100644 --- a/ft_isalnum.c +++ b/ft_isalnum.c @@ -6,10 +6,12 @@ /* By: dkaiser = 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' diff --git a/ft_isalpha.c b/ft_isalpha.c index 64d4b9a..054d96c 100644 --- a/ft_isalpha.c +++ b/ft_isalpha.c @@ -6,10 +6,12 @@ /* By: dkaiser = 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) diff --git a/ft_isdigit.c b/ft_isdigit.c index 273bfa3..2601fee 100644 --- a/ft_isdigit.c +++ b/ft_isdigit.c @@ -6,10 +6,12 @@ /* By: dkaiser = '0' && c <= '9') diff --git a/ft_isprint.c b/ft_isprint.c index 6c9faed..c04cf4f 100644 --- a/ft_isprint.c +++ b/ft_isprint.c @@ -6,10 +6,12 @@ /* By: dkaiser = ' ' && c <= '~') diff --git a/ft_memchr.c b/ft_memchr.c index da3b0a6..568faa6 100644 --- a/ft_memchr.c +++ b/ft_memchr.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" void *ft_memchr(const void *s, int c, size_t n) { diff --git a/ft_memcmp.c b/ft_memcmp.c index a1e18da..0bd52df 100644 --- a/ft_memcmp.c +++ b/ft_memcmp.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" int ft_memcmp(const void *s1, const void *s2, size_t n) { diff --git a/ft_memcpy.c b/ft_memcpy.c index 0ca79fa..768d500 100644 --- a/ft_memcpy.c +++ b/ft_memcpy.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" void *ft_memcpy(void *dst, const void *src, size_t n) { diff --git a/ft_memmove.c b/ft_memmove.c index e4aa03e..5d12f20 100644 --- a/ft_memmove.c +++ b/ft_memmove.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" void *ft_memmove(void *dst, const void *src, size_t len) { diff --git a/ft_memset.c b/ft_memset.c index 60a18e9..58c5e1e 100644 --- a/ft_memset.c +++ b/ft_memset.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" void *ft_memset(void *b, int c, size_t len) { diff --git a/ft_putchar_fd.c b/ft_putchar_fd.c index 5a1808d..5334e0e 100644 --- a/ft_putchar_fd.c +++ b/ft_putchar_fd.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" void ft_putchar_fd(char c, int fd) { diff --git a/ft_putendl_fd.c b/ft_putendl_fd.c index 9929875..ab08ec4 100644 --- a/ft_putendl_fd.c +++ b/ft_putendl_fd.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" void ft_putendl_fd(char *s, int fd) { diff --git a/ft_putstr_fd.c b/ft_putstr_fd.c index 3be5a70..4dd472b 100644 --- a/ft_putstr_fd.c +++ b/ft_putstr_fd.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" void ft_putstr_fd(char *s, int fd) { diff --git a/ft_strchr.c b/ft_strchr.c index 0b20bb4..ecf522e 100644 --- a/ft_strchr.c +++ b/ft_strchr.c @@ -6,10 +6,12 @@ /* By: dkaiser */ /* #include */ -/* int main() { */ -/* char str[] = "Hello world"; */ -/* printf("strchr: %s\n", strchr(str, 'o')); */ -/* printf("ft_strchr: %s\n", ft_strchr(str, 'o')); */ + +/* int main(void) */ +/* { */ +/* char str[] = "teste"; */ + +/* printf("strchr: %p\n", strchr(str, '\0')); */ +/* printf("ft_strchr: %p\n", ft_strchr(str, '\0')); */ /* } */ diff --git a/ft_strdup.c b/ft_strdup.c index af7ab12..1282c36 100644 --- a/ft_strdup.c +++ b/ft_strdup.c @@ -6,33 +6,38 @@ /* By: dkaiser +#include "libft.h" char *ft_strdup(const char *s1) { char *result; int len; + int i; len = 0; - while (*(s1++)) + while (s1[len]) len++; result = malloc(len + 1); if (!result) return (0); - result[++len] = '\0'; - while (len--) - result[len] = *(--s1); + result[len] = '\0'; + i = 0; + while (i < len) + { + result[i] = s1[i]; + i++; + } return (result); } /* #include */ /* int main() { */ /* char *output; */ -/* char input[] = "Dies ist ein Test."; */ +/* char input[] = "test"; */ /* output = ft_strdup(input); */ /* printf("%s\n", output); */ /* } */ diff --git a/ft_strjoin.c b/ft_strjoin.c index c399c87..ebcd611 100644 --- a/ft_strjoin.c +++ b/ft_strjoin.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" static int get_len(char const *s) { diff --git a/ft_strlcat.c b/ft_strlcat.c index 0ce0e19..d49dfad 100644 --- a/ft_strlcat.c +++ b/ft_strlcat.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" /* size_t ft_strlcat(char *dst, const char *src, size_t dstsize) */ /* { */ diff --git a/ft_strlcpy.c b/ft_strlcpy.c index b4f6abc..1cc62e5 100644 --- a/ft_strlcpy.c +++ b/ft_strlcpy.c @@ -6,11 +6,11 @@ /* By: dkaiser +#include "libft.h" size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) { diff --git a/ft_strlen.c b/ft_strlen.c index e201765..867c5eb 100644 --- a/ft_strlen.c +++ b/ft_strlen.c @@ -6,10 +6,12 @@ /* By: dkaiser +#include "libft.h" int ft_strncmp(const char *s1, const char *s2, size_t n) { diff --git a/ft_strnstr.c b/ft_strnstr.c index 2b96b64..54868d4 100644 --- a/ft_strnstr.c +++ b/ft_strnstr.c @@ -6,21 +6,23 @@ /* By: dkaiser +#include "libft.h" char *ft_strnstr(const char *haystack, const char *needle, size_t len) { size_t i; size_t k; + if (len < 0 || (!haystack[0] && needle[0])) + return (0); i = 0; if (*needle == '\0' || needle == haystack) return ((char *)haystack); - while (i < len) + while (i < len && haystack[i]) { k = 0; while (haystack[i + k] && haystack[i + k] == needle[k] && i + k < len) @@ -38,9 +40,10 @@ char *ft_strnstr(const char *haystack, const char *needle, size_t len) /* int main(void) */ /* { */ -/* char haystack[] = "M"; */ -/* char needle[] = "MZIRIBMZE"; */ +/* char haystack[] = "abc"; */ +/* char needle[] = "abcde"; */ +/* size_t len = 5; */ -/* printf("strnstr: %s\n", strnstr(haystack, haystack, 2)); */ -/* printf("ft_strnstr: %s\n", ft_strnstr(haystack, haystack, 2)); */ +/* printf("strnstr: %s\n", strnstr(haystack, needle, len)); */ +/* printf("ft_strnstr: %s\n", ft_strnstr(haystack, needle, len)); */ /* } */ diff --git a/ft_strrchr.c b/ft_strrchr.c index ac0d0ab..5d31654 100644 --- a/ft_strrchr.c +++ b/ft_strrchr.c @@ -6,7 +6,7 @@ /* By: dkaiser +#include "libft.h" static int is_in(char c, const char *str) { 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); */ /* } */ diff --git a/ft_tolower.c b/ft_tolower.c index 31e848f..c81b9ef 100644 --- a/ft_tolower.c +++ b/ft_tolower.c @@ -6,10 +6,12 @@ /* By: dkaiser = 'A' && c <= 'Z') diff --git a/ft_toupper.c b/ft_toupper.c index 5082232..51e4ba6 100644 --- a/ft_toupper.c +++ b/ft_toupper.c @@ -6,10 +6,12 @@ /* By: dkaiser = 'a' && c <= 'z') diff --git a/libft.h b/libft.h index 8fa6fd4..e646b9e 100644 --- a/libft.h +++ b/libft.h @@ -6,12 +6,16 @@ /* By: dkaiser +# include +# include int ft_isalpha(int c); int ft_isdigit(int c); -- 2.47.2