ft_strmapi: initial implementation
This commit is contained in:
parent
08bae77518
commit
f2f363518c
3 changed files with 72 additions and 3 deletions
5
Makefile
5
Makefile
|
|
@ -6,7 +6,7 @@
|
|||
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# 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
|
||||
|
||||
|
|
|
|||
66
ft_strmapi.c
Normal file
66
ft_strmapi.c
Normal 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);
|
||||
}
|
||||
}
|
||||
*/
|
||||
4
libft.h
4
libft.h
|
|
@ -6,7 +6,7 @@
|
|||
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue