-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_substr.c
50 lines (47 loc) · 1.7 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
39
40
41
42
43
44
45
46
47
48
49
50
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: vcodrean <vcodrean@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/20 16:29:19 by vcodrean #+# #+# */
/* Updated: 2022/10/01 10:24:26 by vcodrean ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/**
* "ft_substr() returns a pointer to a new string which is a substring of the
* string pointed to by s, starting at index start and of length len."
*
* The first thing we do is check if s is NULL. If it is, we return NULL
*
* param char s The string to be cut.
* param unsigned int start the index of the first character to include in the
* substring.
* param size_t len the length of the substring
*
* return A pointer to a new string.
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *str;
size_t i;
if (!s)
return (NULL);
if (start > ft_strlen(s))
len = 0;
if (ft_strlen(s) < len)
len = ft_strlen(s);
str = (char *)malloc(sizeof(*s) * (len + 1));
if (!str)
return (NULL);
i = 0;
while (i < len)
{
str[i] = s[i + start];
i++;
}
str[i] = '\0';
return (str);
}