correction memcpy pour le cas ou dst et src sont nuls
This commit is contained in:
52
srcs/ft_substr.c
Normal file
52
srcs/ft_substr.c
Normal file
@@ -0,0 +1,52 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_substr.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: hulamy <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/11/25 14:01:58 by hulamy #+# #+# */
|
||||
/* Updated: 2019/11/27 19:54:09 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)
|
||||
{
|
||||
char *str;
|
||||
size_t i;
|
||||
|
||||
if (!s)
|
||||
return (NULL);
|
||||
if (ft_strlen(s) < start)
|
||||
return ("");
|
||||
if (!(str = (char *)malloc(sizeof(char) * (len + 1))))
|
||||
return (NULL);
|
||||
i = 0;
|
||||
while (i < len && s[start])
|
||||
str[i++] = s[start++];
|
||||
str[i] = '\0';
|
||||
return (str);
|
||||
}
|
||||
Reference in New Issue
Block a user