-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_substr.c
More file actions
74 lines (62 loc) · 1.93 KB
/
ft_substr.c
File metadata and controls
74 lines (62 loc) · 1.93 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
ft_substr() returns a substring of the string s passed as parameter.
The ft_substr function takes a string s, a starting index start, and a length
len as parameters. It first checks if the input string is NULL, and if so, it
returns NULL to indicate an invalid input. It then verifies if the starting
index is beyond the length of the string; if true, it returns an empty string.
The function calculates the remaining length of the string after the specified
start index and adjusts the length parameter if it exceeds the remaining length.
Finally, the function allocates memory for a substring, copies the appropriate
portion from the input string, and returns the newly allocated substring.
*/
#include "libft.h"
char *ft_substr(const char*s, unsigned int start, size_t len)
{
char *substr;
size_t remaining_len;
if (!s)
return (NULL);
if (start >= ft_strlen(s))
return (ft_strdup(""));
remaining_len = ft_strlen(s + start);
if (len > remaining_len)
len = remaining_len;
substr = (char *)malloc(sizeof(char) * (len + 1));
if (!substr)
return (NULL);
ft_strlcpy(substr, s + start, len +1);
return (substr);
}
/*
#include <stdio.h>
int main(void)
{
unsigned int start;
size_t len;
char *src_ptr = "We are supercalifragilisticexpialidocious!";
printf("Source: '%s'\n\n",src_ptr);
start = 7;
len = 9;
printf("start: %d\n", start);
printf("length: %zu\n", len);
printf("Substring: '%s'\n\n",ft_substr(src_ptr, start, len));
start = 7;
len = 99;
printf("start: %d\n", start);
printf("length: %zu\n", len);
printf("Substring: '%s'\n\n",ft_substr(src_ptr, start, len));
start = 99;
len = 7;
printf("start: %d\n", start);
printf("length: %zu\n", len);
printf("Substring: '%s'\n\n",ft_substr(src_ptr, start, len));
start = 7;
len = 9;
src_ptr = NULL;
printf("Source: '%s'\n",src_ptr);
printf("start: %d\n", start);
printf("length: %zu\n", len);
printf("Substring: '%s'\n",ft_substr(src_ptr, start, len));
return (0);
}
*/