-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
57 lines (51 loc) · 2.02 KB
/
ft_substr.c
File metadata and controls
57 lines (51 loc) · 2.02 KB
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
51
52
53
54
55
56
57
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sde-silv <sde-silv@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/02 13:52:02 by sde-silv #+# #+# */
/* Updated: 2023/06/08 14:11:56 by sde-silv ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
/*
#include <unistd.h>
s: The string from which to create the substring.
start: The start index of the substring in the string ’s’.
len: The maximum length of the substring.
Return: The substring. OR NULL if the allocation fails.
Desc: Allocates (with malloc(3)) and returns a substring from the string ’s’.
The substring begins at index ’start’ and is of maximum size ’len’.
strlen(s) < start
ft_strdup("") : allocates sufficient memory for a copy of the string s1, does
the copy, and returns a pointer to it.
strlen(s) >= start
strlen(s + start) < len (s is the limiting factor)
len = strlen(s + start)
strlen(s + start) => len (len is the limiting factor)
Allocate mem (len + 1) of char size
*/
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *ptr;
if (!s)
return (0);
if (ft_strlen(s) < start)
return (ft_strdup(""));
if (ft_strlen(s + start) < len)
len = ft_strlen(s + start);
ptr = malloc(sizeof(char) * (len + 1));
if (!ptr)
return (0);
ft_strlcpy(ptr, s + start, len + 1);
return (ptr);
}
/*
int main(void)
{
ft_substr("Hello World", 3, 5);
return (0);
}
*/