-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_substr.c
46 lines (39 loc) · 1.5 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_substr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mzhukova <mzhukova@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/15 14:12:32 by mzhukova #+# #+# */
/* Updated: 2023/11/18 12:22:26 by mzhukova ### ########.fr */
/* */
/* ************************************************************************** */
#include "libft.h"
size_t ft_strlen(const char *s);
char *ft_strdup(const char *s1);
char *ft_substr(char const *s, unsigned int start, size_t len)
{
char *p;
unsigned int i;
if (start > ft_strlen(s))
return (ft_strdup(""));
if ((ft_strlen(s) - start) < len)
len = ft_strlen(s) - start;
i = 0;
p = malloc((len + 1) * sizeof(char));
if (!p)
return (NULL);
while (len-- && s[start] != '\0')
p[i++] = s[start++];
p[i] = '\0';
return (p);
}
// #include <stdio.h>
// int main (void)
// {
// char *s1 = "Hello beautiful world";
// char *result = ft_substr(s1, 9, 8);
// printf("Result: %s\n", result);
// return (0);
// }