Skip to content

É uma bliblioteca em C de uso geral para ser utilizada principalmente em projetos da École 42

License

Notifications You must be signed in to change notification settings

gawbsouza/42-libft

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Libft


Description

Libft is a static C library developed as part of the École 42 curriculum. It reimplements classic libc functions and provides utilities for string, memory, and linked list manipulation, among others.


Table of Contents


Installation

Requirements:

Build the library:

make

Usage

  1. Include the header in your code:

    #include "libft.h"
  2. Compile your code with libft:

    gcc -I ./inc -L . -lft your_code.c -o your_program
    • -I ./inc tells the compiler where the header is.
    • -L . tells where the compiled library (libft.a) is.
    • -lft links with libft.

Available Functions

Category Main functions
Character validation ft_isalpha, ft_isdigit, ft_isalnum, ft_isascii, ft_isprint
Character manipulation ft_toupper, ft_tolower
String manipulation ft_strlen, ft_strdup, ft_strchr, ft_strrchr, ft_strncmp, ft_strjoin, ft_split, ft_strtrim, ft_substr, ft_strlcpy, ft_strlcat, ft_strnstr, ft_striteri, ft_strmapi
Memory manipulation ft_memset, ft_memcpy, ft_memmove, ft_memchr, ft_memcmp, ft_bzero, ft_calloc
Conversion ft_atoi, ft_itoa
Output ft_putchar_fd, ft_putstr_fd, ft_putendl_fd, ft_putnbr_fd
Linked lists ft_lstnew, ft_lstadd_front, ft_lstadd_back, ft_lstsize, ft_lstlast, ft_lstdelone, ft_lstclear, ft_lstiter, ft_lstmap

Character validation

Checks if a character is an alphabetic letter (A-Z or a-z).
Returns 1 if true, 0 otherwise.

Checks if a character is a decimal digit (0-9).
Returns 1 if true, 0 otherwise.

Checks if a character is alphanumeric (letter or digit).
Returns 1 if true, 0 otherwise.

Checks if a character is an ASCII character (0-127).
Returns 1 if true, 0 otherwise.

Checks if a character is printable (ASCII 32-126).
Returns 1 if true, 0 otherwise.


Character manipulation

Converts a lowercase letter to uppercase.
If the character is not lowercase, returns it unchanged.

Converts an uppercase letter to lowercase.
If the character is not uppercase, returns it unchanged.


String manipulation

Calculates the length of a null-terminated string.
Returns the number of characters before the null terminator.

Allocates and returns a new string which is a duplicate of the input.
The memory for the new string is obtained with malloc.

Searches for the first occurrence of a character in a string.
Returns a pointer to the matched character or NULL if not found.

Searches for the last occurrence of a character in a string.
Returns a pointer to the matched character or NULL if not found.

Compares up to n characters of two strings.
Returns the difference between the first differing characters.

Concatenates two strings into a newly allocated string.
Returns the new string or NULL if allocation fails.

Splits a string into an array of strings using a delimiter character.
Returns a NULL-terminated array of newly allocated strings.

Removes all characters in set from the beginning and end of the string.
Returns a new trimmed string or NULL if allocation fails.

Extracts a substring from a string, starting at a given index and length.
Returns a newly allocated substring or an empty string if out of bounds.

Copies up to size - 1 characters from src to dst, NUL-terminating the result.
Returns the total length of src.

Appends src to dst of size size (NUL-terminating the result).
Returns the total length of the string it tried to create.

Locates the first occurrence of the substring little in big, where not more than len characters are searched.
Returns a pointer to the first occurrence or NULL if not found.

Applies a function to each character of a string, passing its index as first argument.
The function is applied in-place to the string.

Creates a new string by applying a function to each character of the input string.
The function receives the index and character as arguments.


Memory manipulation

Fills the first n bytes of the memory area pointed to by mem with the constant byte c.
Returns a pointer to the memory area mem.

Copies n bytes from memory area src to memory area dst.
Returns a pointer to dst.

Copies n bytes from src to dst, handling overlapping memory areas safely.
Returns a pointer to dst.

Scans the first n bytes of the memory area for the character c.
Returns a pointer to the matching byte or NULL if not found.

Compares the first n bytes of two memory areas.
Returns the difference between the first differing bytes.

Erases the data in the n bytes of the memory area by writing zeros.
Does not return a value.

Allocates memory for an array of nmemb elements of size bytes each and sets all bytes to zero.
Returns a pointer to the allocated memory or NULL if allocation fails.


Conversion

Converts a string to an integer, handling optional whitespace and sign.
Returns the converted integer value.

Allocates and returns a string representing the integer n.
Handles negative numbers and zero.


Output

Outputs the character c to the given file descriptor fd.
Does not return a value.

Outputs the string str to the given file descriptor fd.
Does not return a value.

Outputs the string str to the given file descriptor fd, followed by a newline.
Does not return a value.

Outputs the integer n to the given file descriptor fd.
Does not return a value.


Linked lists

Allocates and returns a new list node with the given content.
The next pointer is initialized to NULL.

Adds the node new at the beginning of the list.
Updates the head pointer to the new node.

Adds the node new at the end of the list.
Updates the last node's next pointer.

Counts the number of nodes in a list.
Returns the count as an integer.

Returns the last node of the list.
If the list is empty, returns NULL.

Frees the memory of a single list node using the given delete function.
Does not free the next nodes.

Deletes and frees all nodes of the list using the given delete function.
Sets the head pointer to NULL.

Iterates the list and applies a function to the content of each node.
Does not modify the list structure.

Creates a new list by applying a function to each node's content of the original list.
Uses a delete function to free memory if needed.


Quick Example

#include "libft.h"

int main(void)
{
    char *str = "42";
    int num = ft_atoi(str);

    ft_putstr_fd("Converted number: ", 1);
    ft_putnbr_fd(num, 1);
    ft_putstr_fd("\n", 1);

    char *duplicated = ft_strdup(str);
    ft_putstr_fd("Duplicated string: ", 1);
    ft_putstr_fd(duplicated, 1);
    ft_putstr_fd("\n", 1);

    ft_putstr_fd("Hello, 42!\n", 1);

    return 0;
}

Norminette

All code follows the Norm 3.3.x and has been validated by Norminette.


License

Distributed under the MIT License. See LICENSE for more information.


About

É uma bliblioteca em C de uso geral para ser utilizada principalmente em projetos da École 42

Topics

Resources

License

Stars

Watchers

Forks