ft_substr: initial implementation

This commit is contained in:
Khaïs COLIN 2024-10-17 12:00:30 +02:00
parent a20611c09a
commit 7d896b939c
3 changed files with 44 additions and 3 deletions

View file

@ -6,7 +6,7 @@
# By: kcolin <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2024/10/14 13:43:59 by kcolin #+# #+# #
# Updated: 2024/10/17 10:52:23 by kcolin ### ########.fr #
# Updated: 2024/10/17 11:44:01 by kcolin ### ########.fr #
# #
# **************************************************************************** #
@ -34,7 +34,8 @@ SOURCES = ft_isalpha.c \
ft_strnstr.c \
ft_atoi.c \
ft_calloc.c \
ft_strdup.c
ft_strdup.c \
ft_substr.c
OBJECTS = $(SOURCES:.c=.o)
CC = gcc

39
ft_substr.c Normal file
View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/17 11:45:52 by kcolin #+# #+# */
/* Updated: 2024/10/17 11:58:18 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
#include <stdlib.h>
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *out;
out = malloc(sizeof(char) * len + 1);
if (out == NULL)
return (NULL);
ft_strlcpy(out, s + start, len + 1);
return (out);
}
/*
#include <stdio.h> // BAD
int main(void)
{
char *data = "This is a long test string.";
printf("'%s'\n", ft_substr(data, 0, 4));
printf("'%s'\n", ft_substr(data, 15, 4));
printf("'%s'\n", ft_substr(data, 15, 0));
printf("'%s'\n", ft_substr(data, 20, 20));
}
*/

View file

@ -6,7 +6,7 @@
/* By: kcolin <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/15 10:11:54 by kcolin #+# #+# */
/* Updated: 2024/10/17 11:03:30 by kcolin ### ########.fr */
/* Updated: 2024/10/17 11:40:51 by kcolin ### ########.fr */
/* */
/* ************************************************************************** */
@ -47,5 +47,6 @@ int ft_atoi(const char *nptr);
void *ft_calloc(size_t nmemb, size_t size);
char *ft_strdup(const char *s);
char *ft_substr(char const *s, unsigned int start, size_t len);
#endif