From 10cf338cbf96b97bcae2f6d854a601481f5e37a1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Wed, 16 Oct 2024 10:52:03 +0200 Subject: [PATCH] ft_strrchr: intial implementation --- Makefile | 5 +++-- ft_strrchr.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ libft.h | 1 + 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 ft_strrchr.c diff --git a/Makefile b/Makefile index 82c94c7..6e0274e 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/16 10:46:03 by kcolin ### ########.fr # +# Updated: 2024/10/16 10:46:46 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -26,7 +26,8 @@ SOURCES = ft_isalpha.c \ ft_strlcat.c \ ft_toupper.c \ ft_tolower.c \ - ft_strchr.c + ft_strchr.c \ + ft_strrchr.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_strrchr.c b/ft_strrchr.c new file mode 100644 index 0000000..74870ab --- /dev/null +++ b/ft_strrchr.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strrchr.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/16 10:34:23 by kcolin #+# #+# */ +/* Updated: 2024/10/16 10:48:57 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +char *ft_strrchr(const char *s, int c) +{ + int i; + + i = ft_strlen(s); + while (i != 0) + { + i--; + if (s[i] == c) + return ((char *)s + i); + } + return (0); +} + +/* +#include + +int main(int argc, char **argv) +{ + char *result; + + if (argc > 2) + { + result = ft_strrchr(argv[1], argv[2][0]); + if (result == 0) + printf("(null)\n"); + else + printf("%s\n", result); + } + return (0); +} +*/ diff --git a/libft.h b/libft.h index 188b3e1..c287d7f 100644 --- a/libft.h +++ b/libft.h @@ -34,5 +34,6 @@ int ft_toupper(int c); int ft_tolower(int c); char *ft_strchr(const char *s, int c); +char *ft_strrchr(const char *s, int c); #endif