-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsub.c
40 lines (36 loc) · 1.5 KB
/
ft_strsub.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
39
40
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vinguyen <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/10/10 23:27:10 by vinguyen #+# #+# */
/* Updated: 2019/10/10 23:27:12 by vinguyen ### ########.fr */
/* */
/* ************************************************************************** */
/*
** Alloc using malloc(3) and returns new substring. Substring begins with
** index start and is of size len. If start and len aren't referring to a valid
** substring, the behavior is undefined. If the alloc fails, return NULL
** Param: string, start index of substring, size of substring
** Return: substring
*/
#include "libft.h"
char *ft_strsub(char const *s, unsigned int start, size_t len)
{
char *substr;
size_t i;
i = 0;
substr = (char*)malloc(sizeof(char) * (len + 1));
if (!s || !substr)
return (NULL);
while (i < len && s[start])
{
substr[i] = s[start];
start++;
i++;
}
substr[i] = '\0';
return (substr);
}