"Why use standard functions when you can build your own?" - Every 42 Student Ever 😄
Hey there! 👋 Welcome to my very first project at 42 School. This is libft
- where I built my own C library from scratch (because apparently printf
is too mainstream 😎).
Scored a sweet 125/100 by implementing not just the required functions, but also nailing all the bonus parts. Think of it as the Swiss Army knife of C functions that I'll be using throughout my 42 journey!
This project consists of coding a C library. It contains a lot of general-purpose functions that will be a useful foundation for future 42 projects.
Function | Description | Prototype |
---|---|---|
ft_isalpha | Check if character is alphabetic | int ft_isalpha(int c) |
ft_isdigit | Check if character is digit | int ft_isdigit(int c) |
ft_isalnum | Check if character is alphanumeric | int ft_isalnum(int c) |
ft_isascii | Check if character is ASCII | int ft_isascii(int c) |
ft_isprint | Check if character is printable | int ft_isprint(int c) |
ft_strlen | Calculate length of string | size_t ft_strlen(const char *s) |
ft_memset | Fill memory with constant byte | void *ft_memset(void *b, int c, size_t len) |
ft_bzero | Zero a byte string | void ft_bzero(void *s, size_t n) |
ft_memcpy | Copy memory area | void *ft_memcpy(void *dst, const void *src, size_t n) |
ft_memmove | Copy memory area with overlap handling | void *ft_memmove(void *dst, const void *src, size_t len) |
ft_strlcpy | Size-bounded string copying | size_t ft_strlcpy(char *dst, const char *src, size_t dstsize) |
ft_strlcat | Size-bounded string concatenation | size_t ft_strlcat(char *dst, const char *src, size_t dstsize) |
ft_toupper | Convert char to uppercase | int ft_toupper(int c) |
ft_tolower | Convert char to lowercase | int ft_tolower(int c) |
ft_strchr | Locate character in string | char *ft_strchr(const char *s, int c) |
ft_strrchr | Locate character in string from end | char *ft_strrchr(const char *s, int c) |
ft_strncmp | Compare two strings | int ft_strncmp(const char *s1, const char *s2, size_t n) |
ft_memchr | Scan memory for a character | void *ft_memchr(const void *s, int c, size_t n) |
ft_memcmp | Compare memory areas | int ft_memcmp(const void *s1, const void *s2, size_t n) |
ft_strnstr | Locate a substring in a string | char *ft_strnstr(const char *haystack, const char *needle, size_t len) |
ft_atoi | Convert ASCII string to integer | int ft_atoi(const char *str) |
ft_calloc | Allocate and zero-initialize array | void *ft_calloc(size_t count, size_t size) |
ft_strdup | Create duplicate of string | char *ft_strdup(const char *s1) |
Function | Description | Prototype |
---|---|---|
ft_substr | Extract substring from string | char *ft_substr(char const *s, unsigned int start, size_t len) |
ft_strjoin | Concatenate two strings | char *ft_strjoin(char const *s1, char const *s2) |
ft_strtrim | Trim characters from string | char *ft_strtrim(char const *s1, char const *set) |
ft_split | Split string using delimiter | char **ft_split(char const *s, char c) |
ft_itoa | Convert integer to ASCII string | char *ft_itoa(int n) |
ft_strmapi | Apply function to string | char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) |
ft_striteri | Apply function to string with index | void ft_striteri(char *s, void (*f)(unsigned int, char*)) |
ft_putchar_fd | Output char to file descriptor | void ft_putchar_fd(char c, int fd) |
ft_putstr_fd | Output string to file descriptor | void ft_putstr_fd(char *s, int fd) |
ft_putendl_fd | Output string with newline to fd | void ft_putendl_fd(char *s, int fd) |
ft_putnbr_fd | Output integer to file descriptor | void ft_putnbr_fd(int n, int fd) |
Function | Description | Prototype |
---|---|---|
ft_lstnew | Create new list node | t_list *ft_lstnew(void *content) |
ft_lstadd_front | Add node at beginning | void ft_lstadd_front(t_list **lst, t_list *new) |
ft_lstsize | Count list elements | int ft_lstsize(t_list *lst) |
ft_lstlast | Get last node of list | t_list *ft_lstlast(t_list *lst) |
ft_lstadd_back | Add node at end | void ft_lstadd_back(t_list **lst, t_list *new) |
ft_lstdelone | Delete node content | void ft_lstdelone(t_list *lst, void (*del)(void *)) |
ft_lstclear | Delete and free list | void ft_lstclear(t_list **lst, void (*del)(void *)) |
ft_lstiter | Apply function to list content | void ft_lstiter(t_list *lst, void (*f)(void *)) |
ft_lstmap | Apply function and create new list | t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) |
- GCC compiler
- Make
- Git
- Clone the repository:
git clone https://github.com/yomazini/42cursus-libft.git
- Navigate to the project directory:
cd 42cursus-libft
- Compile the library:
# Compile mandatory functions
make
# Compile with bonus functions
make bonus
# Clean object files
make clean
# Clean everything (objects and library)
make fclean
# Recompile everything
make re
Through this project, I gained deep understanding of:
- Memory Management: Deep dive into how memory works in C
- Data Structures: Understanding and implementing linked lists
- Algorithm Optimization: Writing efficient and optimized code
- String Manipulation: Advanced string handling techniques
- Pointer Arithmetic: Complex pointer manipulations and memory operations
- Code Organization: Structuring a library project
- Documentation: Writing clear and useful documentation
- Memory Safety: All functions handle edge cases and prevent buffer overflows
- Performance: Optimized for speed and efficiency
- Reliability: Thoroughly tested against edge cases
- Compliance: Follows 42's strict norm requirements
- Zero Memory Leaks: Validated using
leaks
- Well Documented: Clear comments and documentation
The library has been extensively tested using:
- 42's unit tests
- Custom test cases
- War Machine
- Tripouille tester
- Manual edge case testing
.
├── Makefile
├── README.md
├── libft.h
├── ft_atoi.c
├── ft_memset.c
├── ft_split.c
├── ft_lstnew_bonus.c
├── ft_lstmap_bonus.c
└── ...
#include "libft.h"
int main(void)
{
// String manipulation
char *str = ft_strdup("Hello, 42!");
printf("Length: %zu\n", ft_strlen(str));
// Linked list
t_list *lst = ft_lstnew(str);
ft_lstadd_back(&lst, ft_lstnew("World!"));
// Memory operations
char buffer[10];
ft_memset(buffer, 'A', 9);
buffer[9] = '\0';
return (0);
}
This will link the library file to your program. Zhich allow you to use the functions in the library.
gcc -o my_program my_program.c -L. -lft
Made with 💖 and ☕️ by Youssef Mazini (ymazini)
- "Practice makes perfect, but perfect practice makes perfect permanent!"
- Document your code as if the next maintainer is a psychopath who knows where you live
- Test your code like your life depends on it (because your grades do!)
- Keep your code clean, your mind sharp, and your coffee strong
- Remember: A working function is good, but a well-documented, efficient, and clean function is better!
"In code we trust, but tests we must!" 😄