Skip to content

Additional

Iva edited this page Sep 20, 2024 · 12 revisions

Strings

UtilsLists

ft_substr

Description:
Creates a new string by extracting a substring from the string s, starting at the index start and up to len characters long.

Signature char *ft_substr(char const *s, unsigned int start, size_t len);
Parameters s: The string from which to create the substring.
start: The starting index of the substring in the string s.
len: The maximum length of the substring.
Return value A newly allocated substring, or NULL if the allocation fails.
Equivalent The C++ std::string class has the .substr() method to extract substrings.

Usage Example::

char *str = "Hello, World!";
char *substr = ft_substr(str, 7, 5); // Creates a new string "World"

Specifics:

ft_substr allocates memory, so the returned string must be freed after use.

char *copy = ft_substr("Hello");
free(copy); // Freeing the allocated memory

ft_strjoin

Description:
Creates a new string, which is the result of the concatenation of s1 and s2.

Signature char *ft_strjoin(char const *s1, char const *s2);
Parameters s1: The prefix string.
s2: The suffix string.
Return value A newly allocated string resulting from the concatenation of s1 and s2, or NULL if the allocation fails.
Equivalent The C++ std::string class concatenation using the + operator.

Usage Example:

char *s1 = "Hello";
char *s2 = " World!";
char *joined = ft_strjoin(s1, s2); // Creates a new string "Hello World!"

Specifics:

ft_strjoin allocates memory, so the returned string must be freed after use.

char *joined = ft_strjoin("Hello", " World!");
free(joined); // Freeing the allocated memory

ft_strtrim

Description:
Creates a new string, which is a copy of s1 with the characters specified in set removed from the beginning and the end of the string.

Signature char *ft_strtrim(char const *s1, char const *set);
Parameters s1: The string to be trimmed.
set: The reference set of characters to trim.
Return value A newly allocated trimmed string, or NULL if the allocation fails.
Equivalent None.

Usage Example:

char *s1 = "  Hello, World!  ";
char *trimmed = ft_strtrim(s1, " "); // Creates a new string "Hello, World!"

Specifics:

ft_strtrim allocates memory, so the returned string must be freed after use.

char *trimmed = ft_strtrim("  Hello  ", " ");
free(trimmed); // Freeing the allocated memory

ft_split

Description:
Creates an array of strings obtained by splitting s using the character c as a delimiter. The array ends with a NULL pointer.

Signature char **ft_split(char const *s, char c);
Parameters s: The string to be split.
c: The delimiter character.
Return value A newly allocated array of strings resulting from the split, or NULL if the allocation fails.
Equivalent None.

Usage Example:

char **result = ft_split("Hello World Hello", ' '); // Creates new strings: "Hello" "World" "Hello"

Specifics:

ft_split allocates memory for each substring and the array itself. All memory must be freed after use.

char **result = ft_split("Hello World", ' ');
for (int i = 0; result[i] != NULL; i++)
    free(result[i]); // Free each substring
free(result); // Free the array

ft_strmapi

Description:
Applies the function f to each character of the string s to create a new string, resulting from successive applications of f.

Signature char *ft_strmapi(char const *s, char (*f)(unsigned int, char));
Parameters s: The string on which to iterate.
f: The function to apply to each character.
Return value A newly allocated string, or NULL if the allocation fails.
Equivalent None.

Usage Example:

char *result = ft_strmapi("hello", to_uppercase); // Creates a new string "HELLO"

Specifics:

ft_strmapi allocates memory for the new string, so the returned string must be freed after use.

char *result = ft_strmapi("abc", to_uppercase);
free(result); // Freeing the allocated memory

ft_striteri

Description:
Applies the function f to each character of the string s, passing its index as the first argument. Each character is passed by reference to f to be modified if necessary.

Signature void ft_striteri(char *s, void (*f)(unsigned int, char *));
Parameters s: The string on which to iterate.
f: The function to apply to each character, taking its index and the character as arguments.
Return value None.
Equivalent None.

Usage Example:

char str[] = "hello";
ft_striteri(str, to_uppercase); // Transform passed string to "HELLO"

Specifics:

Since ft_striteri modifies the string s in-place, no need to free any memory after use.


Utils

StringsLists Top ⬆️

ft_itoa

Description:
Creates a string representing the integer received as an argument.

Signature char *ft_itoa(int n);
Parameters n: The integer to convert into a string.
Return value A newly allocated string representing the integer. Returns NULL if the allocation fails.
Equivalent C++11 have similar behavior using methods like std::to_string() to convert integers to strings.

Usage Example:

char *str = ft_itoa(1234); // Converts the integer 1234 into the string "1234"

Specifics:

ft_itoa allocates memory for the string, so the returned string must be freed after use.

char *str = ft_itoa(42);
free(str); // Freeing the allocated memory

ft_putchar_fd

Description:
Writes the character c to the given file descriptor.

Signature void ft_putchar_fd(char c, int fd);
Parameters c: The character to output.
fd: The file descriptor on which to write.
Return value None.
Equivalent In C, the write(2) function can be used to write directly to a file descriptor.

Usage Example:

ft_putchar_fd('A', 1); // Outputs 'A' to the standard output (file descriptor 1)

ft_putstr_fd

Description:
Writes the string s to the given file descriptor.

Signature void ft_putstr_fd(char *s, int fd);
Parameters s: The string to output.
fd: The file descriptor on which to write.
Return value None.
Equivalent None.

Usage Example:

ft_putstr_fd("Hello, World!", 1); // Outputs "Hello, World!" to standard output (fd 1)

ft_putendl_fd

Description:
Writes the string s to the given file descriptor, followed by a newline.

Signature void ft_putendl_fd(char *s, int fd);
Parameters s: The string to output.
fd: The file descriptor on which to write.
Return value None.
Equivalent None.

Usage Example:

ft_putendl_fd("Hello, World!", 1); // Outputs "Hello, World!\n" to standard output (fd 1)

ft_putnbr_fd

Description:
Writes the integer n to the given file descriptor.

Signature void ft_putnbr_fd(int n, int fd);
Parameters n: The integer to output.
fd: The file descriptor on which to write.
Return value None.
Equivalent None.

Usage Example:

ft_putnbr_fd(1234, 1); // Outputs "1234" to standard output (fd 1)

Lists

StringsUtils Top ⬆️

ft_lstnew

Description:
Creates a new element of a linked list. The variable content is initialized with the value of the parameter content. The next pointer is initialized to NULL.

Signature t_list *ft_lstnew(void *content);
Parameters content: The content to store in the new list element.
Return value A newly allocated list element. Returns NULL if the allocation fails.
Equivalent The C++ std::list STL automatically manages memory and pointers for a linked list.

Usage Example:

t_list *new_node = ft_lstnew("Hello"); // Creates a node with "Hello" content ((char *)new_node->content)

Specifics:

The returned list element must be freed after use.

free(new_node);

ft_lstadd_front

Description:
Adds the element new at the beginning of the list lst.

Signature void ft_lstadd_front(t_list **lst, t_list *new);
Parameters lst: A pointer to the first element of the list.
new: The new element to add.
Return value None.
Equivalent The C++ std::list class has the .push_front() method to add elements to the front of the list.

Usage Example:

t_list *head = ft_lstnew("first");
t_list *new_node = ft_lstnew("second");
ft_lstadd_front(&head, new_node); // switches node "head" to node "new_node".
// Now new_node is the head, (second, first).

ft_lstadd_back

Description:
Adds the element new at the end of the list lst.

Signature void ft_lstadd_back(t_list **lst, t_list *new);
Parameters lst: A pointer to the first element of the list.
new: The new element to add at the end.
Return value None.
Equivalent The C++ std::list class has the .push_back() method to add elements to the back of the list.

Usage Example:

t_list *head = ft_lstnew("first");
t_list *new_node = ft_lstnew("second");
ft_lstadd_back(&head, new_node);
// Now new_node is the last of the linked list, (first, second).
t_list *other_node = ft_lstnew("third");
ft_lstadd_back(&head, other_node);
// Now other_node is the last of the linked list, (first, second, third).

ft_lstsize

Description:
Counts the number of elements in a list.

Signature int ft_lstsize(t_list *lst);
Parameters lst: The beginning of the list.
Return value The number of elements in the list.
Equivalent The C++ std::list class has the .size() method to return the number of elements in the list.

Usage Example:

t_list *head = ft_lstnew("first");
ft_lstadd_back(&head, ft_lstnew("second"));
int size = ft_lstsize(head); // Returns 2

ft_lstlast

Description:
Returns the last element of the list.

Signature t_list *ft_lstlast(t_list *lst);
Parameters lst: The beginning of the list.
Return value The last element of the list. Returns NULL if the list is empty.
Equivalent The C++ std::list class has the .back() method to access the last element of the list.

Usage Example:

t_list *head = ft_lstnew("first");
ft_lstadd_back(&head, ft_lstnew("second"));
t_list *last = ft_lstlast(head); // Returns the last node, "second"

ft_lstdelone

Description:
Takes as a parameter an element and frees the memory of the element's content using the function del and free the element itself.

Signature void ft_lstdelone(t_list *lst, void (*del)(void *));
Parameters lst: The element to free.
del: A function used to free the content of the element.
Return value None.
Equivalent None.

Usage Example:

void del_content(void *content)
{
    free(content);
}

t_list *node = ft_lstnew(strdup("node"));
ft_lstdelone(node, del_content); // Frees the node and its content, removing node from list

ft_lstclear

Description:
Deletes and frees the memory of all the elements in the list starting from lst using the function del and frees the list itself. Finally, the pointer to the list is set to NULL.

Signature void ft_lstclear(t_list **lst, void (*del)(void *));
Parameters lst: The address of a pointer to the first element of the list.
del: A function used to free the content of each element.
Return value None.
Equivalent The C++ std::list class has the .clear() method to remove all elements from the list and free memory.

Usage Example:

void del_content(void *content)
{
    free(content);
}

t_list *head = ft_lstnew(strdup("node1"));
ft_lstadd_back(&head, ft_lstnew(strdup("node2")));
ft_lstclear(&head, del_content); // Applies "del" function to each node content int the list, and frees all nodes too

ft_lstiter

Description:
Iterates through the list lst and applies the function f to the content of each element.

Signature void ft_lstiter(t_list *lst, void (*f)(void *));
Parameters lst: The address of a pointer to the first element of the list.
f: The function to apply to the content of each element.
Return value None.
Equivalent The C++ std::for_each algorithm can be used to apply a function to each element in a std::list.

Usage Example:

void print_content(void *content)
{
    printf("%s\n", (char *)content);
}

t_list *head = ft_lstnew(strdup("node1"));
ft_lstadd_back(&head, ft_lstnew(strdup("node2")));
ft_lstiter(head, print_content); // Applies "print_content" function to each node int the list, outputs: "node1" "node2"

ft_lstmap

Description:
Iterates through the list lst and applies the function f to the content of each element, creating a new list resulting from the successive applications of f. The del function is used to delete the content of an element if necessary.

Signature t_list *ft_lstmap(t_list *lst, void *(*f)(void *), void (*del)(void *));
Parameters lst: A pointer to the first element of the list.
f: The function to apply to the content of each element.
del: A function to free the content if necessary.
Return value A new list resulting from the successive applications of f, or NULL if allocation fails.
Equivalent The C++ std::transform algorithm can be used to apply a function to each element in a std::list.

Usage Example:

t_list *head = ft_lstnew(strdup("node1"));
ft_lstadd_back(&head, ft_lstnew(strdup("node2")));
t_list *new_list = ft_lstmap(head, toupper, del_content); // Creates new list with nodes "NODE1" "NODE2"

Previous ⬅️ Top ⬆️