ft_strmapi: initial implementation

This commit is contained in:
Khaïs COLIN 2024-10-18 11:55:40 +02:00
parent 08bae77518
commit f2f363518c
3 changed files with 72 additions and 3 deletions

View file

@ -6,7 +6,7 @@
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ # # By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2024/10/14 13:43:59 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_strjoin.c \
ft_strtrim.c \ ft_strtrim.c \
ft_split.c \ ft_split.c \
ft_itoa.c ft_itoa.c \
ft_strmapi.c
OBJECTS = $(SOURCES:.c=.o) OBJECTS = $(SOURCES:.c=.o)
CC = gcc CC = gcc

66
ft_strmapi.c Normal file
View file

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/18 11:34:37 by kcolin #+# #+# */
/* Updated: 2024/10/18 11:52:51 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
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 <stdio.h>
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 <text...>\n", argv[0]);
return (1);
}
}
*/

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */ /* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 10:11:54 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_itoa(int n);
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
#endif #endif