maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
ft_substr.c
Go to the documentation of this file.
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* ft_substr.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: rmikhayl <rmikhayl@student.42london.c +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2023/12/17 17:25:27 by rmikhayl #+# #+# */
9
/* Updated: 2023/12/17 17:25:27 by rmikhayl ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "
libft.h
"
14
15
/* *************************** ft_substr ************************************ */
16
/* Extracts a substring from 's' starting at the specified 'start' */
17
/* position and up to 'len' characters. Returns a new string with the */
18
/* extracted content or NULL if memory allocation fails or 'start' is */
19
/* out of bounds. */
20
/* */
21
/* In layman's terms: It's like cutting a portion of a sentence from a */
22
/* specific point and up to a certain length to create a new shorter */
23
/* sentence. If something goes wrong or 'start' is too far away, you get */
24
/* nothing. */
25
/* ************************************************************************** */
26
27
char
*
ft_substr
(
char
const
*s,
unsigned
int
start,
size_t
len)
28
{
29
char
*new_str;
30
size_t
i;
31
size_t
j;
32
size_t
size;
33
34
size =
ft_strlen
(s) - start;
35
if
(start >
ft_strlen
(s))
36
size = 0;
37
else
if
(size > len)
38
size = len;
39
new_str = (
char
*)malloc(size + 1);
40
if
(!s || !new_str)
41
return
(0);
42
i = start;
43
j = 0;
44
while
(i <
ft_strlen
(s) && j < len)
45
new_str[j++] = s[i++];
46
new_str[j] =
'\0'
;
47
return
(new_str);
48
}
ft_substr
char * ft_substr(char const *s, unsigned int start, size_t len)
Definition
ft_substr.c:27
libft.h
ft_strlen
size_t ft_strlen(const char *s)
Definition
ft_strlen.c:15
lib
libft
src
ft_substr.c
Generated by
1.9.8