-
Notifications
You must be signed in to change notification settings - Fork 1
/
ft_substr.c
47 lines (43 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cjackows <cjackows@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/08 22:23:07 by cjackows #+# #+# */
/* Updated: 2023/05/24 10:06:35 by cjackows ### ########.fr */
/* */
/* ************************************************************************** */
#include "../inc/libft.h"
/**
* @brief Allocates (with malloc) and returns a substring from the string 's'.
* The substring begins at index ’start’ and is of maximum size ’len’.
* @param s The string from which to create the substring.
* @param start The start index of the substring in the string ’s’.
* @param len The maximum length of the substring.
* @return char* to allocated with subtring memory.
*/
char *ft_substr(char *s, unsigned int start, size_t len)
{
char *str;
size_t i;
i = 0;
if (!s)
return (NULL);
if (ft_strlen(s) < len)
len = ft_strlen(s);
str = malloc(sizeof(char) * (len + 1));
while (start <= ft_strlen(s) && i < len)
{
str[i] = s[i + start];
i ++;
}
str[i] = '\0';
return (str);
}
// int main(void)
// {
// const char s[] = "1234567890";
// printf("%s\n", ft_substr(s, 0, 4));
// }