From f2f363518ce7b8fc3dedbceffdb8b94811e3bd9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 18 Oct 2024 11:55:40 +0200 Subject: [PATCH] ft_strmapi: initial implementation --- Makefile | 5 ++-- ft_strmapi.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++ libft.h | 4 +++- 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 ft_strmapi.c diff --git a/Makefile b/Makefile index 40dd6e8..ad46b57 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: kcolin +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2024/10/14 13:43:59 by kcolin #+# #+# # -# Updated: 2024/10/17 17:07:32 by kcolin ### ########.fr # +# Updated: 2024/10/18 11:33:48 by kcolin ### ########.fr # # # # **************************************************************************** # @@ -39,7 +39,8 @@ SOURCES = ft_isalpha.c \ ft_strjoin.c \ ft_strtrim.c \ ft_split.c \ - ft_itoa.c + ft_itoa.c \ + ft_strmapi.c OBJECTS = $(SOURCES:.c=.o) CC = gcc diff --git a/ft_strmapi.c b/ft_strmapi.c new file mode 100644 index 0000000..5f4b5a3 --- /dev/null +++ b/ft_strmapi.c @@ -0,0 +1,66 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_strmapi.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: kcolin +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/18 11:34:37 by kcolin #+# #+# */ +/* Updated: 2024/10/18 11:52:51 by kcolin ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include + +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) +{ + unsigned int i; + char *out; + + out = ft_calloc(ft_strlen(s) + 1, sizeof(char)); + if (out == NULL) + return (NULL); + i = 0; + while (s[i] != '\0') + { + out[i] = (*f)(i, s[i]); + i++; + } + return (out); +} + +/* +#include + +char upcase_some_letters(unsigned int i, char c) +{ + if (i % 2 == 0) + return (ft_toupper(c)); + else + return (ft_tolower(c)); +} + + +int main(int argc, char **argv) +{ + if (argc > 1) + { + int i = 1; + + while (i < argc) + { + char *out = ft_strmapi(argv[i], &upcase_some_letters); + printf("%d\t'%s'\n", i, out); + free(out); + i++; + } + return (0); + } + else + { + printf("Usage: %s \n", argv[0]); + return (1); + } +} +*/ diff --git a/libft.h b/libft.h index f00186e..0421dd6 100644 --- a/libft.h +++ b/libft.h @@ -6,7 +6,7 @@ /* By: kcolin +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */ -/* Updated: 2024/10/17 11:40:51 by kcolin ### ########.fr */ +/* Updated: 2024/10/18 11:33:58 by kcolin ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,4 +54,6 @@ char **ft_split(char const *s, char c); char *ft_itoa(int n); +char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); + #endif