Skip to content

luaraggio/libft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Libft

Table of Contents

Program Name

libft.a

Files

  • Makefile
  • libft.h
  • ft_*.c (all your implementation files)

Makefile

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.

External Functions

N/A

Description

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.

Technical Considerations

  • 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.

Part 1 - Libc Functions

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_.

Functions to Implement

  • isalpha
  • isdigit
  • isalnum
  • isascii
  • isprint
  • strlen
  • memset
  • bzero
  • memcpy
  • memmove
  • strlcpy
  • strlcat
  • toupper
  • tolower
  • strchr
  • strrchr
  • strncmp
  • memchr
  • memcmp
  • strnstr
  • atoi
  • calloc (requires malloc())
  • strdup (requires malloc())

Part 2 - Additional Functions

In this second part, you must develop functions that are either not in the libc or are presented in a different form.

Functions to Implement

  • 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.

String Manipulation Library Documentation

Mandatory Functions

Function Name: ft_strlen

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.

Function Name: ft_strcpy

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.

Function Name: ft_strlcpy

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.

Function Name: ft_strchr

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.

Function Name: ft_strrchr

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.

Function Name: ft_strncmp

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.

Function Name: ft_strnstr

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 in haystack.

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.

Function Name: ft_atoi

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.

Function Name: ft_strdup

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.

Function Name: ft_strjoin

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.

Function Name: ft_strtrim

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.

Function Name: ft_split

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.

Libft Bonus Part

Table of Contents

Introduction

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.

Structure Definition

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;

Functions

The following functions will be implemented to manipulate the linked list:

ft_lstnew

Prototype: t_list *ft_lstnew(void *content);
Parameters:

  • content: The content to create the node with.
Return Value: The new node.
External Functions: malloc
Description: Allocates and returns a new node, initializing content and setting next to NULL.

ft_lstadd_front

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.
Return Value: None.
Description: Adds the node new at the beginning of the list.

ft_lstsize

Prototype: int ft_lstsize(t_list *lst);
Parameters:

  • lst: The beginning of the list.
Return Value: The length of the list.
Description: Counts the number of nodes in a list.

ft_lstlast

Prototype: t_list *ft_lstlast(t_list *lst);
Parameters:

  • lst: The beginning of the list.
Return Value: Last node of the list.
Description: Returns the last node of the list.

ft_lstadd_back

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.
Return Value: None.
Description: Adds the node new at the end of the list.

ft_lstdelone

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.
Return Value: None.
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.

ft_lstclear

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.
Return Value: None.
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.

ft_lstiter

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.
Return Value: None.
Description: Iterates the list lst and applies the function f on the content of each node.

ft_lstmap

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.
Return Value: The new list. NULL if the allocation fails.
External Functions: malloc, free
Description: Iterates the list lst and applies the function <

Conclusion

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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published