libft
Loading...
Searching...
No Matches
ft_substr.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* ft_substr.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: tspoof <tspoof@student.hive.fi> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2022/11/03 16:53:11 by tspoof #+# #+# */
9/* Updated: 2022/11/29 14:58:35 by tspoof ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "libft.h"
14
15static size_t ft_min(size_t a, size_t b)
16{
17 if (a < b)
18 return (a);
19 return (b);
20}
21
22char *ft_substr(char const *s, unsigned int start, size_t len)
23{
24 size_t s_len;
25 size_t sub_len;
26 char *ptr;
27
28 if (!s)
29 return (NULL);
30 s_len = ft_strlen(s);
31 if (start < s_len)
32 sub_len = ft_min(s_len, len);
33 else
34 sub_len = 0;
35 ptr = (char *)ft_calloc(sub_len + 1, sizeof(char));
36 if (!ptr)
37 return (NULL);
38 ft_strlcpy(ptr, s + start, sub_len + 1);
39 return (ptr);
40}
char * ft_substr(char const *s, unsigned int start, size_t len)
Gets a piece of string out of a string.
Definition ft_substr.c:22
size_t ft_strlcpy(char *dst, const char *src, size_t dstsize)
Copies string to destination.
Definition ft_strlcpy.c:15
size_t ft_strlen(const char *s)
Length of the string.
Definition ft_strlen.c:15
void * ft_calloc(size_t count, size_t size)
Allocate and zero.
Definition ft_calloc.c:15