- Program Name
- Files
- External Functions
- Description
- Technical Considerations
- Part 1 - Libc Functions
- Part 2 - Additional Functions
libft.a
- Makefile
- libft.h
- ft_*.c (all your implementation files)
The Makefile should contain the following targets:
- NAME: to create the library.
- all: to compile all source files.
- clean: to remove object files.
- fclean: to remove the library and object files.
- re: to recompile everything.
N/A
The libft library is a collection of useful functions designed to support your cursus by providing a set of functionalities similar to those found in the standard C library.
- Declaring global variables is forbidden.
- If you need helper functions to split a more complex function, define them as static functions. This limits their scope to the appropriate file.
- Place all your files at the root of your repository.
- Turning in unused files is forbidden.
- Every .c file must compile with the flags -Wall -Wextra -Werror.
- Use the command ar to create your library. Using the libtool command is forbidden.
- Your libft.a has to be created at the root of your repository.
In this part, you will implement a set of functions from the libc. Your functions will have the same prototypes and behaviors as the originals but will be prefixed with ft_
.
isalpha
isdigit
isalnum
isascii
isprint
strlen
memset
bzero
memcpy
memmove
strlcpy
strlcat
toupper
tolower
strchr
strrchr
strncmp
memchr
memcmp
strnstr
atoi
calloc
(requiresmalloc()
)strdup
(requiresmalloc()
)
In this second part, you must develop functions that are either not in the libc or are presented in a different form.
ft_substr:
char *ft_substr(char const *s, unsigned int start, size_t len);
Allocates and returns a substring from the string s.
ft_strjoin:
char *ft_strjoin(char const *s1, char const *s2);
Allocates and returns a new string, the result of concatenating s1 and s2.
ft_strtrim:
char *ft_strtrim(char const *s1, char const *set);
Allocates and returns a copy of s1 with specified characters trimmed from the beginning and end.
ft_split:
char **ft_split(char const *s, char c);
Allocates and returns an array of strings split from s using c as a delimiter.
ft_itoa:
char *ft_itoa(int n);
Allocates and returns a string representing the integer n.
ft_strmapi:
char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
Applies function f to each character of the string s and creates a new string.
ft_striteri:
void ft_striteri(char *s, void (*f)(unsigned int, char*));
Applies function f on each character of s.
ft_putchar_fd:
void ft_putchar_fd(char c, int fd);
Outputs the character c to the given file descriptor.
ft_putstr_fd:
void ft_putstr_fd(char *s, int fd);
Outputs the string s to the given file descriptor.
ft_putendl_fd:
void ft_putendl_fd(char *s, int fd);
Outputs the string s followed by a newline to the given file descriptor.
ft_putnbr_fd:
void ft_putnbr_fd(int n, int fd);
Outputs the integer n to the given file descriptor.
Prototype: size_t ft_strlen(const char *s);
Parameters:
s
: The string to be measured.
Return: The length of the string excluding the null terminator.
External Functions: None
Description: Computes the length of the string s
.
Prototype: char *ft_strcpy(char *dest, const char *src);
Parameters:
dest
: The destination string.src
: The source string.
Return: A pointer to the destination string dest
.
External Functions: None
Description: Copies the string src
to dest
including the null terminator.
Prototype: size_t ft_strlcpy(char *dest, const char *src, size_t size);
Parameters:
dest
: The pointer to the destination.src
: The pointer to the source.size
: The total size of the destination buffer.
Return: The total length of the string that would be created.
External Functions: ft_strlen
, ft_strlcat
Description: Concatenates the string src
to the end of the string dest
. Copies are made up to size - 1
characters, and a null
terminator is added.
Prototype: char *ft_strchr(const char *s, int c);
Parameters:
s
: The string to be searched.c
: The character to be located.
Return: A pointer to the first occurrence of character c
in the string s
, or NULL
if the character is not found.
External Functions: None
Description: Locates the first character c
in the string s
.
Prototype: char *ft_strrchr(const char *s, int c);
Parameters:
s
: The string to be searched.c
: The character to be located.
Return: A pointer to the last occurrence of character c
in the string s
, or NULL
if the character is not found.
External Functions: None
Description: Locates the last occurrence of character c
in the string s
.
Prototype: int ft_strncmp(const char *s1, const char *s2, size_t n);
Parameters:
s1
: The first string.s2
: The second string.n
: The maximum number of characters to compare.
Return: An integer less than, equal to, or greater than zero, depending on the comparison.
External Functions: None
Description: Compares the first n
letters of two strings s1
and s2
.
Prototype: char *ft_strnstr(const char *haystack, const char *needle, size_t len);
Parameters:
haystack
: The string to be searched.needle
: The string to be found.len
: The maximum number of characters to be examined inhaystack
.
Return: A pointer to the first occurrence of needle
in haystack
, or NULL
if not found.
External Functions: None
Description: Locates the first occurrence of needle
within haystack
, limiting the search to len
characters.
Prototype: int ft_atoi(const char *nptr);
Parameters:
nptr
: The string to be converted.
Return: The integer represented by the string.
External Functions: None
Description: Converts the string nptr
to an integer.
Prototype: char *ft_strdup(const char *s1);
Parameters:
s1
: The string to be duplicated.
Return: A pointer to the new duplicated string, or NULL
on failure.
External Functions: malloc
, ft_strlen
Description: Creates a new string that is a duplicate of s1
.
Prototype: char *ft_strjoin(char const *s1, char const *s2);
Parameters:
s1
: The first string to be concatenated.s2
: The second string to be concatenated.
Return: A pointer to the new concatenated string, or NULL
on failure.
External Functions: malloc
, ft_strlen
Description: Creates a new string that is the concatenation of s1
and s2
.
Prototype: char *ft_strtrim(char const *s1, char const *set);
Parameters:
s1
: The string to be trimmed.set
: The string containing the characters to be removed.
Return: A pointer to the new trimmed string, or NULL
on failure.
External Functions: malloc
, ft_strlen
Description: Creates a new string by removing characters from string s1
that are contained in set
at both the start and the end.
Prototype: char **ft_split(char const *s, char c);
Parameters:
s
: The string to be split.c
: The delimiter character.
Return: A pointer to an array of strings, or NULL
on failure.
External Functions: malloc
, ft_strlen
Description: Splits the string s
into substrings using the character c
as a delimiter.
The bonus part of the Libft project focuses on implementing a linked list data structure to improve memory and string manipulation. This additional functionality will enhance the library and provide bonus points if successfully completed after the mandatory part is perfected.
Add the following structure definition to your libft.h
file:
typedef struct s_list
{
void *content; // Data contained in the node.
struct s_list *next; // Address of the next node or NULL.
} t_list;
The following functions will be implemented to manipulate the linked list:
Prototype: t_list *ft_lstnew(void *content);
Parameters:
content
: The content to create the node with.
External Functions:
malloc
Description: Allocates and returns a new node, initializing
content
and setting next
to NULL
.
Prototype: void ft_lstadd_front(t_list **lst, t_list *new);
Parameters:
lst
: The address of a pointer to the first link of a list.new
: The address of a pointer to the node to be added to the list.
Description: Adds the node
new
at the beginning of the list.
Prototype: int ft_lstsize(t_list *lst);
Parameters:
lst
: The beginning of the list.
Description: Counts the number of nodes in a list.
Prototype: t_list *ft_lstlast(t_list *lst);
Parameters:
lst
: The beginning of the list.
Description: Returns the last node of the list.
Prototype: void ft_lstadd_back(t_list **lst, t_list *new);
Parameters:
lst
: The address of a pointer to the first link of a list.new
: The address of a pointer to the node to be added to the list.
Description: Adds the node
new
at the end of the list.
Prototype: void ft_lstdelone(t_list *lst, void (*del)(void *));
Parameters:
lst
: The node to free.del
: The address of the function used to delete the content.
External Functions:
free
Description: Takes as a parameter a node and frees the memory of the node’s content using the function
del
given as a parameter and frees the node. The memory of next
must not be freed.
Prototype: void ft_lstclear(t_list **lst, void (*del)(void *));
Parameters:
lst
: The address of a pointer to a node.del
: The address of the function used to delete the content of the node.
External Functions:
free
Description: Deletes and frees the given node and every successor of that node, using the function
del
and free(3)
. Finally, the pointer to the list must be set to NULL
.
Prototype: void ft_lstiter(t_list *lst, void (*f)(void *));
Parameters:
lst
: The address of a pointer to a node.f
: The address of the function used to iterate on the list.
Description: Iterates the list
lst
and applies the function f
on the content of each node.
Prototype: t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
Parameters:
lst
: The address of a pointer to a node.f
: The address of the function used to iterate on the list.del
: The address of the function used to delete the content of a node if needed.
NULL
if the allocation fails.External Functions:
malloc
, free
Description: Iterates the list
lst
and applies the function <
This documentation describes the functions implemented in a string manipulation library. Each function has been detailed with its prototype, parameters, return value, and description. The implementation of these functions is essential for efficient string manipulation in C.