Libft is the 1st project of the Common Core curriculum at 42. It's about coding a C library: specifically, recoding a few functions of the C standard library, as well as some other utility functions. Future projects will rely on this library.
Usage · Subject · Part 1 · Part 2 · Bonus · License
-
Clone repository
git clone git@github.com:teresa-chow/42-libft.git
-
Go inside project directory and run
makecd libft make -
To use the library in your code,
#includethe following header#include "libft.h"
| Command | Purpose |
|---|---|
make |
to compile libft with mandatory files |
make bonus |
to compile libft with bonus |
make clean |
to clean the working directory of object files *.o |
make fclean |
to clean the working directory of object and archive files *.o / *.a |
make re |
to clean and re-compile |
Note
This codebase follows the applicable programming standard at 42, known as the Norm.
Recoding of C standard library functions
| Name | Prototype | Description |
|---|---|---|
| isalpha | int ft_isalpha(int c); |
check for alphabetic character |
| isdigit | int ft_isdigit(int c); |
check for digit (0 through 9) |
| isalnum | int ft_isalnum(int c); |
check for alphanumeric character |
| isascii | int ft_isascii(int c); |
check whether c is a 7-bit unsigned char that fits into the ASCII character set |
| isprint | int ft_isprint(int c); |
check for any printable character (including SPACE) |
| strlen | size_t ft_strlen(const char *s); |
calculate the length of a string |
| memset | void *ft_memset(void *s, int c, size_t n); |
fill memory with a constant byte |
| bzero | void ft_bzero(void *s, size_t n); |
write zero-valued bytes |
| memcpy | void *ft_memcpy(void *dest, const void *src, size_t n); |
copy memory area (can only copy in forward direction) |
| memmove | void *ft_memmove(void *dest, const void *src, size_t n); |
copy memory area (can copy in both forward and backward direction) |
| strlcpy | size_t ft_strlcpy(char *dst, const char *src, size_t size); |
size-bounded string copying |
| strlcat | size_t ft_strlcat(char *dst, const char *src, size_t size); |
size-bounded string concatenation |
| toupper | int ft_toupper(int c); |
convert a lowercase letter to uppercase |
| tolower | int ft_tolower(int c); |
convert an uppercase letter to lowercase |
| strchr | char *ft_strchr(const char *s, int c) |
locate a character in a string (1st occurrence) |
| strrchr | char *ft_strrchr(const char *s, int c) |
locate a character in a string (last occurrence) |
| strncmp | int ft_strncmp(const char *s1, const char *s2, size_t n) |
compare two strings |
| memchr | void *ft_memchr(const void *s, int c, size_t n) |
scan memory for a character |
| strnstr | char *ft_strnstr(const char *big, const char *little, size_t len) |
locate a substring in a string |
| atoi | int ft_atoi(const char *nptr) |
convert a string to an integer |
| calloc | void *ft_calloc(size_t nmemb, size_t size); |
allocate dynamic memory: the memory is set to zero |
| strdup | char *ft_strdup(const char *s); |
duplicate a string |
Coding of modified standard or non-standard utility functions
| Name | Prototype | Description |
|---|---|---|
| ft_substr | char *ft_substr(char const *s, unsigned int start, size_t len); |
allocate with malloc() and return a substring from string s (beginning at index start and of max. size len) |
| ft_strjoin | char *ft_strjoin(char const *s1, char const *s2); |
allocate with malloc() and return a new string, which is the result of the concatenation of s1 and s2 |
| ft_strtrim | char *ft_strtrim(char const *s1, char const *set); |
allocate with malloc() and return a copy of s1 with the characters specified in set removed from the beginning and the end of the string |
| ft_split | char **ft_split(char const *s, char c); |
allocate with malloc() and return an array of strings obtained by splitting s using the character c as a delimiter. The array must end with a NULL pointer |
| ft_itoa | char *ft_itoa(int n); |
allocate with malloc() and return a string representing the integer (positive, zero or negative) received as an argument |
| ft_strmapi | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); |
apply function f to each character of the string s, and passing its index as first argument to create a new string (with malloc()) resulting from successive applications of f |
| ft_striteri | void ft_striteri(char *s, void (*f)(unsigned int, char*)); |
apply function f on each character of the string s passed as argument, passing its index as first argument |
| ft_putchar_fd | void ft_putchar_fd(char c, int fd); |
output character c to given file descriptor fd |
| ft_putstr_fd | void ft_putstr_fd(char *s, int fd); |
output string s to given file descriptor fd |
| ft_putendl_fd | void ft_putendl_fd(char *s, int fd); |
output string s to given file descriptor, followed by a newline |
| ft_putnbr_fd | void ft_putnbr_fd(int n, int fd); |
output integer n to given file descriptor fd |
Manipulating lists
| Name | Prototype | Description |
|---|---|---|
| ft_lstnew | t_list *ft_lstnew(void *content); |
allocate with malloc() and return a new node. Member variable content is initialized with the value of the parameter content; variable next is initialized to NULL |
| ft_lstadd_front | void ft_lstadd_front(t_list **lst, t_list *new); |
add new node at the beginning of the list |
| ft_lstsize | int ft_lstsize(t_list *lst); |
count number of nodes in a list |
| ft_lstlast | t_list *ft_lstlast(t_list *lst); |
return last node of the list |
| ft_lstadd_back | void ft_lstadd_back(t_list **lst, t_list *new); |
add new node at end of the list |
| ft_lstdelone | void ft_lstdelone(t_list *lst, void (*del)(void*)); |
free the memory of node lst’s content using function del given as a parameter and free() the node; note: memory of next must not be freed |
| ft_lstclear | void ft_lstclear(t_list **lst, void (*del)(void*)); |
delete and free node *lst and every successor of that node, using the function del and free(); finally, the pointer to the list must be set to NULL |
| ft_lstiter | void ft_lstiter(t_list *lst, void (*f)(void *)); |
iterate the list lst and apply function f on the content of each node |
| ft_lstmap | t_list *ft_lstmap(t_list *lst, void *(*f)(void *),void (*del)(void *)); |
iterate list lst and apply function f on the content of each node; create a new list resulting of the successive applications of function f; del function is used to delete the content of a node if needed |
This work is published under the terms of 42 Unlicense.