-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_strsub_bonus.c
42 lines (38 loc) · 1.53 KB
/
ft_strsub_bonus.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
41
42
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_strsub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: aviholai <aviholai@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/01/18 13:23:16 by aviholai #+# #+# */
/* Updated: 2022/02/10 14:43:48 by aviholai ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
** 'Strsub()' (substring) creates a new string of an applied string, beginning
** from the mark of integer 'start', the length of size_t 'len'.
** Enough memory is allocated for the substring by the size of 'len' plus one.
** If allocation fails it will return NULL.
*/
char *ft_strsub(char const *s, unsigned int start, size_t len)
{
char *substring;
unsigned int index;
if (s)
{
substring = (char *)malloc(sizeof(char) * len + 1);
if (substring == NULL)
return (NULL);
index = 0;
while (index < len)
{
substring[index] = s[start + index];
index++;
}
substring[index] = '\0';
return (substring);
}
return (NULL);
}