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

Binary file not shown.

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]);

View File

@@ -1,15 +1,3 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 17:26:06 by hulamy #+# #+# */
/* Updated: 2019/11/19 18:15:32 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a copy of s without the firsts and lasts empty characters
*/
@@ -17,6 +5,47 @@
/*
** #include <libc.h>
**
** char *ft_substr(char const *s, unsigned int start, size_t len)
** {
** char *str;
** size_t i;
**
** if (!s)
** return (NULL);
** if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
** return (NULL);
** str[len] = '\0';
** i = 0;
** while (i < len)
** str[i++] = s[start++];
** return (str);
** }
**
** size_t ft_strlen(const char *str)
** {
** size_t i;
**
** i = 0;
** while (str[i])
** i++;
** return (i);
** }
**
** char *ft_strchr(const char *s, int c)
** {
** int i;
** int j;
**
** i = 0;
** j = -1;
** while (s[i])
** i++;
** while (++j < i + 1)
** if (s[j] == c)
** return ((char *)s + j);
** return (NULL);
** }
**
** char *ft_strtrim(char const *s1, char const *set);
**
** int main(int ac, char **av)
@@ -34,7 +63,6 @@ char *ft_strtrim(char const *s1, char const *set)
{
int len;
char *str;
(void)set;
if (!s1)
return (NULL);
@@ -44,16 +72,7 @@ char *ft_strtrim(char const *s1, char const *set)
while (len >= 0 && ft_strchr(set, s1[len]))
len--;
len++;
if (!(str = ft_strsub(s1, 0, len)))
if (!(str = ft_substr(s1, 0, len)))
return (NULL);
return (str);
}

View File

@@ -1,19 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/11/19 17:20:51 by hulamy #+# #+# */
/* Updated: 2019/11/19 17:24:10 by hulamy ### ########.fr */
/* */
/* ************************************************************************** */
/*
** create a copy of a portion of s, begining at start and of length len
*/
/*
** #include <libc.h>
**
** char *ft_substr(char const *s, unsigned int start, size_t len);
**
** int main(int ac, char **av)
** {
** char *str;
**
** if (ac != 4)
** return (0);
** str = ft_substr(av[1], atoi(av[2]), atoi(av[3]));
** printf("%s\n",str);
** return (0);
** }
*/
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
@@ -23,8 +28,9 @@ char *ft_substr(char const *s, unsigned int start, size_t len)
if (!s)
return (NULL);
if (!(str = ft_strnew(len)))
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
return (NULL);
str[len] = '\0';
i = 0;
while (i < len)
str[i++] = s[start++];