Skip to content
/ LIBFT Public

Custom C standard library implementation - Comprehensive collection of essential functions with memory management and string operations

cadenegr/LIBFT

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

LIBFT

A custom C library implementing standard library functions

42 norminette

πŸ“‹ Table of Contents


πŸ“– About

Libft is the first project at 42 School where students recreate essential C standard library functions from scratch. This project serves as the foundation for all future C projects at 42, teaching low-level programming concepts, memory management, and the implementation details of commonly used functions.

The library provides a comprehensive set of utility functions for string manipulation, memory management, character validation, linked list operations, and more.

🎯 Project Objectives

  • Understand standard library functions by implementing them from scratch
  • Learn memory management and proper allocation/deallocation practices
  • Master pointer manipulation and understanding of C data structures
  • Develop debugging skills and attention to detail
  • Follow strict coding standards (42 Norm)
  • Create reusable code that can be used in future 42 projects

πŸ”§ Functions Implemented

Part 1: Standard Library Functions

Recreation of existing C standard library functions

Character Classification & Conversion

Function Description
ft_isalpha Check if character is alphabetic
ft_isdigit Check if character is a digit
ft_isalnum Check if character is alphanumeric
ft_isascii Check if character is ASCII
ft_isprint Check if character is printable
ft_toupper Convert to uppercase
ft_tolower Convert to lowercase

String Functions

Function Description
ft_strlen Calculate string length
ft_strchr Locate character in string
ft_strrchr Locate character in string (reverse)
ft_strncmp Compare strings up to n characters
ft_strlcpy Size-bounded string copying
ft_strlcat Size-bounded string concatenation
ft_strnstr Locate substring in string
ft_atoi Convert string to integer

Memory Functions

Function Description
ft_memset Fill memory with constant byte
ft_bzero Zero a byte string
ft_memcpy Copy memory area
ft_memmove Copy memory area (handles overlap)
ft_memchr Scan memory for character
ft_memcmp Compare memory areas
ft_calloc Allocate and zero memory
ft_strdup Duplicate string

Part 2: Additional Functions

New functions not in the standard library

Function Description
ft_substr Extract substring
ft_strjoin Concatenate two strings
ft_strtrim Trim characters from string
ft_split Split string by delimiter
ft_itoa Convert integer to string
ft_strmapi Apply function to each character
ft_striteri Apply function to each character (with index)
ft_putchar_fd Output character to file descriptor
ft_putstr_fd Output string to file descriptor
ft_putendl_fd Output string + newline to file descriptor
ft_putnbr_fd Output number to file descriptor

Bonus: Linked List Functions

Functions for manipulating linked lists

Function Description
ft_lstnew Create new list element
ft_lstadd_front Add element at beginning of list
ft_lstsize Count elements in list
ft_lstlast Return last element of list
ft_lstadd_back Add element at end of list
ft_lstdelone Delete single element
ft_lstclear Delete and free list
ft_lstiter Iterate through list
ft_lstmap Apply function to each element

πŸ›  Compilation

Requirements

  • GCC compiler
  • Make

Build Library

# Compile the main library
make

# Compile with bonus functions
make bonus

# Clean object files
make clean

# Clean everything (objects + library)
make fclean

# Recompile everything
make re

Compiler Flags

The library is compiled with strict flags to ensure code quality:

-Wall -Werror -Wextra

πŸš€ Usage

Basic Usage

  1. Include the header in your C file:
#include "libft.h"
  1. Compile your program with the library:
gcc -Wall -Wextra -Werror your_program.c -L. -lft -o your_program

Example Program

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

int main(void)
{
    char *str = "Hello, 42!";
    char *substr;
    char **split_result;
    int i;

    // String length
    printf("Length of '%s': %zu\n", str, ft_strlen(str));
    
    // Substring extraction
    substr = ft_substr(str, 7, 2);
    printf("Substring: %s\n", substr);
    free(substr);
    
    // String splitting
    split_result = ft_split(str, ' ');
    i = 0;
    while (split_result[i])
    {
        printf("Word %d: %s\n", i, split_result[i]);
        free(split_result[i]);
        i++;
    }
    free(split_result);
    
    return (0);
}

βœ… Testing

Manual Testing

Create test files to verify function behavior:

# Create a simple test file
cat > test_libft.c << 'EOF'
#include "libft.h"
#include <stdio.h>
#include <string.h>

int main(void)
{
    // Test ft_strlen vs strlen
    char *test = "Test string";
    printf("ft_strlen: %zu, strlen: %zu\n", ft_strlen(test), strlen(test));
    
    // Test ft_strchr vs strchr
    printf("ft_strchr: %s, strchr: %s\n", ft_strchr(test, 's'), strchr(test, 's'));
    
    return (0);
}
EOF

# Compile and run
gcc -Wall -Wextra -Werror test_libft.c -L. -lft -o test_libft
./test_libft

Third-Party Testers

Popular testing frameworks for libft:


πŸ“ Project Structure

libft/
β”œβ”€β”€ Makefile              # Build configuration
β”œβ”€β”€ libft.h               # Header file with function prototypes
β”œβ”€β”€ README.md             # Project documentation
β”œβ”€β”€ ft_*.c                # Source files for each function
└── libft.a               # Compiled static library (after make)

Function Categories

  • ft_is*.c - Character classification functions
  • ft_to*.c - Character conversion functions
  • ft_str*.c - String manipulation functions
  • ft_mem*.c - Memory operation functions
  • ft_put*.c - Output functions
  • ft_lst*.c - Linked list functions (bonus)

πŸ“ 42 School Standards

This project follows the strict 42 Norm coding standard:

  • βœ… Maximum 25 lines per function
  • βœ… Maximum 80 characters per line
  • βœ… No global variables
  • βœ… No for loops (only while)
  • βœ… Proper variable declarations
  • βœ… Consistent indentation and formatting
  • βœ… Comprehensive error handling
  • βœ… No memory leaks

Memory Management

All functions that allocate memory:

  • Return NULL on allocation failure
  • Properly handle edge cases
  • Are tested for memory leaks with valgrind

πŸŽ“ Skills Developed

Through this project, I gained experience in:

  • Low-level C programming and system calls
  • Memory management and debugging memory issues
  • Algorithm implementation and optimization
  • Code organization and modular programming
  • Testing methodologies and edge case handling
  • Documentation and code maintenance

πŸ“ License

This project is part of the 42 School curriculum. Code is provided for educational purposes.


Author: cadenegr
School: 42 Berlin
Project: libft
Date: June 2023

About

Custom C standard library implementation - Comprehensive collection of essential functions with memory management and string operations

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published