Skip to content

A custom C library that reimplements essential standard functions from scratch to use in future projects.

Notifications You must be signed in to change notification settings

davidagredano/libft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

103 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Libft

libft-portada

Libft is a custom C library that reimplements essential standard functions from scratch. The goal is to understand how these functions work internally and build a personal toolkit for future projects.

This library includes functions for handling strings, memory management, type conversions, linked lists, and more.

Project requirements

  • The project must be written in accordance with The Norm, a programming standard that defines a set of rules to follow when writing code. Among others:
    • The use of for, do…while, switch, case, goto and ternary operator is forbidden.
    • Functions should be short (25 lines) and just do one thing.
    • Maximum 4 parameters per function.
    • Only 5 functions per file.
    • Only one instruction or control structure per line.
    • Lines shouldn’t be longer than 80 columns wide.
    • Declaration and initialization cannot be on the same line.
    • All identifiers (functions, types, variables, etc.) names should be explicit and make sense.
  • Functions should not quit unexpectedly (segmentation fault, bus error, double free, etc) apart from undefined behaviors.
  • All heap allocated memory space must be properly freed when necessary. No leaks are tolerated.
  • A Makefile which will compile your source files to the required output must be submitted.
  • All files should be placed at the root of the repository.
  • Every .c files must be compiled with the flags -Wall -Wextra -Werror.
  • The command ar must be used to create the library. Using the libtool command is forbidden.
  • The libft.a has to be created at the root of the repository.
  • Only the write(), malloc(), and free() standard C functions are allowed, no other standard library functions can be used.

Included functions

Character classification

  • ft_isupper - Checks if a character is an uppercase letter (A-Z).
  • ft_islower - Checks if a character is an lowercase letter (a-z).
  • ft_isalpha - Checks if a character is alphabetic (A-Z or a-z).
  • ft_isdigit - Checks if a character is a decimal digit (0-9).
  • ft_isalnum - Checks if a character is alphanumeric (a-z, A-Z, 0-9).
  • ft_isascii - Checks if a character is within ASCII range (0-127).
  • ft_isprint - Checks if a character is printable (32-126).
  • ft_isspace - Checks if a character is a whitespace character.
  • ft_toupper - Converts a lowercase letter to uppercase.
  • ft_tolower - Converts an uppercase letter to lowercase.

Memory manipulation

  • ft_memset - Fills a block of memory with a specified byte value.
  • ft_bzero - Fills a block of memory with zeros.
  • ft_memcpy - Copies bytes from src to dest memory area.
  • ft_memmove - Copies bytes from src to dest.
  • ft_memchr - Locates the first occurrence of a byte in a memory area.
  • ft_memcmp - Compares two memory areas byte by byte.
  • ft_calloc - Allocates memory and initializes all bytes to zero.

String manipulation

  • ft_strlen - Calculates the length of a string.
  • ft_strnlen - Calculates the length of a string without exceeding the specified maxlen.
  • ft_strlcpy - Copies up to size-1 characters from src to dest, NUL-terminating the result.
  • ft_strlcat - Appends a string to the end of another string, ensuring NUL-termination within size bytes.
  • ft_strchr - Finds the first occurrence of a character in a string.
  • ft_strrchr - Finds the last occurrence of a character in a string.
  • ft_strcmp - Compares two strings.
  • ft_strncmp - Compares up to n characters of two strings.
  • ft_strnstr - Searches for a substring in a string up to len characters.
  • ft_strdup - Duplicates a string by allocating enough memory and copying the content.
  • ft_substr - Extracts a substring from a string starting at index start and of length len.
  • ft_strjoin - Concatenates two strings into a new string.
  • ft_strtrim - Removes all characters from the beginning and end of a string
  • ft_split - Splits a string into an array of substrings using a delimiter character.
  • ft_strtok - Extracts tokens from a string using the delimiter characters.
  • ft_strmapi - Creates a new string with by applying a function to the original string.
  • ft_striteri - Applies a function to each character of a string.

Type conversion

  • ft_atoi - Converts a string to an integer.
  • ft_itoa - Converts an integer to a string representation.
  • ft_uitoa - Converts an unsigned integer to a lowercase string representation.
  • ft_uitoa_caps - Converts an unsigned integer to an uppercase string representation.
  • ft_ultoa - Converts an unsigned long to a lowercase string representation.

Print functions

  • ft_putchar - Writes a single character to the standard output.
  • ft_putchar_fd - Writes a single character to the specified file descriptor.
  • ft_putstr - Writes a string to the standard output
  • ft_putstr_fd - Writes a string to the specified file descriptor.
  • ft_putendl_fd - Writes a string to the specified file descriptor, followed by a newline.
  • ft_putnbr_fd - Writes an integer to the specified file descriptor.

Linked list manipulation

  • ft_lstnew - Creates a new list element with the given content.
  • ft_lstadd_front - Adds a new element at the beginning of a list.
  • ft_lstsize - Counts the number of element in a list.
  • ft_lstlast - Returns the last element of the list.
  • ft_lstadd_back - Adds a new element at the end of a list.
  • ft_lstdelone - Deletes a single element from a list, freeing its memory.
  • ft_lstclear - Deletes and frees the list element pointed to, as well as all following elements.
  • ft_lstiter - Applies the given function to the content of each element.
  • ft_lstmap - Creates a new list by applying a function to the content of each element.

Compilation instructions

  1. Download or clone this repository.

    git clone https://github.com/davidagredano/libft.git
  2. Move into the project's directory.

    cd libft
  3. Compile the library. The binary libft.a will be created at the root of the directory.

    make

Other useful commands

Command Description
make Compile the library.
make clean Remove the objects files.
make fclean Remove the objects files and the library.
make re Recompile the library.

Library usage

To use the library functions, include its header to you source files:

#include "path/to/libft.h"

and add the required flags when compiling your code:

gcc [option...] infile... -L<path/to/libft/dir> -lft
  • -l<library>

    The linker searches for the library named library, which is actually a file named lib<library>.a. For example, for our Libft we should use -lft since our library is called libft.a. GCC manual

  • -L<dir>

    Add directory dir to the list of directories to be searched for -l. GCC manual

Usage example

Directory structure:

├── libft/
│   ├── Makefile
│   ├── ft_atoi.c
│   ├── ...
│   ├── ft_ultoa.c
│   ├── libft.a
│   └── libft.h
└── example_project/
    └── main.c

/example_project/main.c content:

#include "../libft/libft.h"

int main() {
    char *str = "Hello, Libft!";
    ft_putendl_fd(str, 1);
}

Compilation including the library:

cd example_project
gcc -o usage_example main.c -L../libft -lft 

Binary execution:

$> ./usage_example
Hello, Libft!
$>

If you like this project, a ⭐️ would be greatly appreciated.
Thanks for your support!

Contributing guidelines

Contributions are always welcome! If you find a bug or have an idea to improve this project, feel free to:

  • Open an issue to report a problem or suggest an enhancement.
  • Submit a pull request if you’ve already implemented a fix or improvement—I’d love to review it!

How to Contribute

  • Fork the repository.
  • Create a new branch (git checkout -b feature/YourFeature).
  • Make your changes and commit (git commit -m "Add YourFeature").
  • Push to your branch (git push origin feature/YourFeature).
  • Open a pull request and describe your changes.

Contact me

Hello! 👋 I'm David, the creator of this project. If you have any questions, suggestions, or just want to chat about software development or anything else, feel free to reach out to me.

My email is davidagredano@gmail.com.

Looking forward to hearing from you!