mise a jour functions strtrim strmapi et substr

This commit is contained in:
Hugo LAMY
2019-11-25 13:52:27 +01:00
parent c29be1b311
commit 500c4516b1
4 changed files with 103 additions and 51 deletions

View File

@@ -1,31 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strmapi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2018/11/14 21:18:03 by hulamy #+# #+# */
/* Updated: 2019/03/25 15:28:21 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a new array with the result of function f
** on every element of s by index i
** create a new array with the result of function f on every element of
** s by index i
*/
/*
** #include <libc.h>
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
** char touppercase(unsigned int i, char c)
** {
** if (i < 3)
** c -= 32;
** return (c);
** }
**
** char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
**
** int main(int ac, char **av)
** {
** char *str;
** char touppercase(unsigned int, char);
**
** if (ac != 2)
** return (0);
** str = av[1];
** printf("%s\n",str);
** str = ft_strmapi(str, touppercase);
** printf("%s\n",str);
** return (0);
** }
*/
#include "libft.h"
char *ft_strmapi(char const *s, char (*f)(unsigned int, char))
{
char *str;
int i;
int size;
if (!s)
return (NULL);
if (!(str = ft_strnew(ft_strlen(s))))
size = ft_strlen(s);
if (!(str = (char *)malloc(sizeof(char) * (size + 1))))
return (NULL);
str[size] = '\0';
i = -1;
while (s[++i])
str[i] = f(i, s[i]);