Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

110 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

This project has been created as part of the 42 curriculum by dneves-d.

📚 libft

The foundational framework of the 42 curriculum.
An implementation of a custom C library replicating standard system behaviors and dynamic data structures.

Language Build Norm


📋 Description

The Libft project serves as the introduction to the imperative programming pipeline at 42. Its objective is to re-code a core selection of functions from the standard C library (libc), alongside a set of custom algorithmic string utilities and linked list manipulation structures.

Developing this library from scratch enforces a rigorous understanding of memory allocation models, raw pointer manipulation, boundary checking, and compilation flags. The compiled static archive (libft.a) acts as a persistent resource used across subsequent school assignments (such as get_next_line, ft_printf, and push_swap).


🛠️ Instructions

Prerequisites

  • A C compiler (cc)
  • GNU make and ar utilities

Installation & Compilation

Clone your repository and navigate to the root directory. The project is managed by a strict Makefile configured with -Wall -Wextra -Werror to prevent errors or illegal relinking routines.

# Compile all functions.
make

# Remove intermediate object files (.o) safely
make clean

# Purge all object files and the final libft.a static archive
make fclean

# Reset the build pipeline completely
make re

Project example:

Creat a 'main.c' file:

#include "libft.h"
#include <stdio.h>

size_t count_words(char **words)
{
    size_t  i;

    if(!words)
        return (-1);

    i = 0;
    while(*words)
    {
        words++;
        i++;
    }
    return (i);
}

int	main(void)
{
    char const  *str = "  Hello 42 Brazil, testing   ";
    char        *clean_str;
    char        **words;
    int         i;

    printf("Original str: %s\n", str);

    // Using 'ft_strtrim' to remove spaces from beginning and end of the text.'
    clean_str = ft_strtrim(str, " ");
    if (!clean_str)
        return (1);
    
    printf("Str no spaces: %s\n", clean_str);

    //Using 'ft_split' to split the text in separated words.
    words = ft_split(clean_str, ' ');
    if(!words)
    {
        free(clean_str);
        return (1);
    }

    printf("\nWas found %lu words.\n\n", count_words(words));

    //Using 'ft_putstr_fd' to print the words.
    i = 0;
    while (words[i] != NULL)
    {
        
        ft_putstr_fd(words[i], 1);
        ft_putstr_fd("\n", 1);
        free(words[i]);
        i++;
    }
    
    return (0);
}

At the terminal type:

make
gcc main.c -L. libft.a -o my_program
./my_program

🗂️ Library Architecture & Layout. The toolkit functions are separated logically across three explicit components:

1. Library C Functions (Part 1)

Character classification

All return 1 if the character belongs to the tested class, 0 otherwise.

Function Description
int ft_isalpha(int c) Checks if c is a letter (a-z, A-Z)
int ft_isdigit(int c) Checks if c is a digit (0-9)
int ft_isalnum(int c) Checks if c is a letter or digit
int ft_isascii(int c) Checks if c is in the ASCII table (0–127)
int ft_isprint(int c) Checks if c is a printable character

Character conversion

Function Description
int ft_toupper(int c) Converts lowercase to uppercase
int ft_tolower(int c) Converts uppercase to lowercase

String manipulation

Function Description
size_t ft_strlen(const char *s) Returns the length of the string
char *ft_strchr(const char *s, int c) First occurrence of c in s
char *ft_strrchr(const char *s, int c) Last occurrence of c in s
int ft_strncmp(const char *s1, const char *s2, size_t n) Compares up to n bytes of two strings
char *ft_strnstr(const char *h, const char *n, size_t len) Finds needle in haystack within len bytes
size_t ft_strlcpy(char *dst, const char *src, size_t size) Safe string copy with NUL-termination guarantee
size_t ft_strlcat(char *dst, const char *src, size_t size) Safe string concatenation with NUL-termination guarantee

Memory

Function Description
void *ft_memset(void *s, int c, size_t n) Fills n bytes with value c
void ft_bzero(void *s, size_t n) Zeroes n bytes
void *ft_memcpy(void *dst, const void *src, size_t n) Copies n bytes (no overlap)
void *ft_memmove(void *dst, const void *src, size_t n) Copies n bytes (overlap-safe)
void *ft_memchr(const void *s, int c, size_t n) Finds byte c in the first n bytes
int ft_memcmp(const void *s1, const void *s2, size_t n) Compares n bytes of two memory regions

Numeric conversion and allocation

Function Description
int ft_atoi(const char *nptr) Converts string to integer
void *ft_calloc(size_t nmemb, size_t size) Allocates and zeroes memory
char *ft_strdup(const char *s) Duplicates a string into allocated memory

2. Additional Functions (Part 2)

Function Description
char *ft_substr(char const *s, unsigned int start, size_t len) Extracts substring from s starting at start, up to len chars
char *ft_strjoin(char const *s1, char const *s2) Concatenates s1 and s2 into a new allocated string
char *ft_strtrim(char const *s1, char const *set) Removes chars from set at both ends of s1
char **ft_split(char const *s, char c) Splits s by delimiter c, returns NULL-terminated array
char *ft_itoa(int n) Converts integer to string (handles negatives)
char *ft_strmapi(char const *s, char (*f)(unsigned int, char)) Applies f to each char, returns new string
void ft_striteri(char *s, void (*f)(unsigned int, char*)) Applies f in-place to each char of s
void ft_putchar_fd(char c, int fd) Writes c to file descriptor fd
void ft_putstr_fd(char *s, int fd) Writes string s to fd
void ft_putendl_fd(char *s, int fd) Writes s followed by newline to fd
void ft_putnbr_fd(int n, int fd) Writes integer n to fd

3. Linked List Utility Functions

A collection of node tools leveraging custom structure declarations (t_list) to orchestrate programmatic dynamic structures smoothly:

The t_list structure:

typedef struct s_list
{
    void            *content;
    struct s_list   *next;
}   t_list;
Function Description
t_list *ft_lstnew(void *content) Creates a new node with content
void ft_lstadd_front(t_list **lst, t_list *new) Inserts new node at the beginning of the list
void ft_lstadd_back(t_list **lst, t_list *new) Inserts new node at the end of the list
int ft_lstsize(t_list *lst) Counts the number of nodes
t_list *ft_lstlast(t_list *lst) Returns the last node
void ft_lstdelone(t_list *lst, void (*del)(void *)) Free one node (does not free the next)
void ft_lstclear(t_list **lst, void (*del)(void *)) Frees the entire list and sets pointer to NULL
void ft_lstiter(t_list *lst, void (*f)(void *)) Applies a function to each node's content
t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *)) Creates a new list by applying a function to each node

🗃️ Resources:

Documentation & Articles:

🤖 AI Usage Disclaimer:

As per the 42 ICT training framework rules, AI tools were utilized responsibly during this project:

  • Tasks Assisted: Conceptual explanations of static pointer arrays, understanding memory management edge cases during errors, and structuring documentation guidelines.

  • Control Practices: No direct code generation or copy-pasting shortcuts were utilized. Code logic was built manually from foundational reasoning and peer collaboration to prepare for internet-free examination conditions. All function implementations were written manually.

42 School Brazil · dneves-d · 06 / 2026

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages