-
Notifications
You must be signed in to change notification settings - Fork 12
/
ft_substr.c
38 lines (34 loc) · 1.41 KB
/
ft_substr.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: apuchill <apuchill@student.42sp.org.br> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/27 21:24:14 by apuchill #+# #+# */
/* Updated: 2020/02/19 14:06:58 by apuchill ### ########.fr */
/* */
/* ************************************************************************** */
/*
** LIBRARY: N/A
** SYNOPSIS: extract substring from string
**
** DESCRIPTION:
** Allocates (with malloc(3)) and returns a substring from the string ’s’.
** The substring begins at index ’start’ and is of maximum size ’len’.
*/
#include "libft.h"
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *new_str;
size_t i;
size_t j;
if (!s || !(new_str = (char *)malloc(len + 1)))
return (0);
i = start;
j = 0;
while (i < ft_strlen(s) && j < len)
new_str[j++] = s[i++];
new_str[j] = '\0';
return (new_str);
}