-
Notifications
You must be signed in to change notification settings - Fork 0
Additional
Description:
Creates a new string by extracting a substring from the strings, starting at the indexstartand up tolencharacters 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. |
char *str = "Hello, World!";
char *substr = ft_substr(str, 7, 5); // Creates a new string "World"
ft_substrallocates memory, so the returned string must be freed after use.
char *copy = ft_substr("Hello");
free(copy); // Freeing the allocated memoryDescription:
Creates a new string, which is the result of the concatenation ofs1ands2.
| 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. |
char *s1 = "Hello";
char *s2 = " World!";
char *joined = ft_strjoin(s1, s2); // Creates a new string "Hello World!"
ft_strjoinallocates memory, so the returned string must be freed after use.
char *joined = ft_strjoin("Hello", " World!");
free(joined); // Freeing the allocated memoryDescription:
Creates a new string, which is a copy ofs1with the characters specified insetremoved 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. |
char *s1 = " Hello, World! ";
char *trimmed = ft_strtrim(s1, " "); // Creates a new string "Hello, World!"
ft_strtrimallocates memory, so the returned string must be freed after use.
char *trimmed = ft_strtrim(" Hello ", " ");
free(trimmed); // Freeing the allocated memoryDescription:
Creates an array of strings obtained by splittingsusing the charactercas a delimiter. The array ends with aNULLpointer.
| 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. |
char **result = ft_split("Hello World Hello", ' '); // Creates new strings: "Hello" "World" "Hello"
ft_splitallocates 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 arrayDescription:
Applies the functionfto each character of the stringsto create a new string, resulting from successive applications off.
| 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. |
char *result = ft_strmapi("hello", to_uppercase); // Creates a new string "HELLO"
ft_strmapiallocates 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 memoryDescription:
Applies the functionfto each character of the strings, passing its index as the first argument. Each character is passed by reference tofto 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. |
char str[] = "hello";
ft_striteri(str, to_uppercase); // Transform passed string to "HELLO"Since
ft_striterimodifies the stringsin-place, no need to free any memory after use.
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. |
char *str = ft_itoa(1234); // Converts the integer 1234 into the string "1234"
ft_itoaallocates memory for the string, so the returned string must be freed after use.
char *str = ft_itoa(42);
free(str); // Freeing the allocated memoryDescription:
Writes the charactercto 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. |
ft_putchar_fd('A', 1); // Outputs 'A' to the standard output (file descriptor 1)Description:
Writes the stringsto 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. |
ft_putstr_fd("Hello, World!", 1); // Outputs "Hello, World!" to standard output (fd 1)Description:
Writes the stringsto 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. |
ft_putendl_fd("Hello, World!", 1); // Outputs "Hello, World!\n" to standard output (fd 1)Description:
Writes the integernto 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. |
ft_putnbr_fd(1234, 1); // Outputs "1234" to standard output (fd 1)Description:
Creates a new element of a linked list. The variablecontentis initialized with the value of the parametercontent. Thenextpointer is initialized toNULL.
| 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. |
t_list *new_node = ft_lstnew("Hello"); // Creates a node with "Hello" content ((char *)new_node->content)The returned list element must be freed after use.
free(new_node);Description:
Adds the elementnewat the beginning of the listlst.
| 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. |
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).Description:
Adds the elementnewat the end of the listlst.
| 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. |
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).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. |
t_list *head = ft_lstnew("first");
ft_lstadd_back(&head, ft_lstnew("second"));
int size = ft_lstsize(head); // Returns 2Description:
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. |
t_list *head = ft_lstnew("first");
ft_lstadd_back(&head, ft_lstnew("second"));
t_list *last = ft_lstlast(head); // Returns the last node, "second"Description:
Takes as a parameter an element and frees the memory of the element's content using the functiondeland 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. |
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 listDescription:
Deletes and frees the memory of all the elements in the list starting fromlstusing the functiondeland frees the list itself. Finally, the pointer to the list is set toNULL.
| 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. |
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 tooDescription:
Iterates through the listlstand applies the functionfto 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. |
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"Description:
Iterates through the listlstand applies the functionfto the content of each element, creating a new list resulting from the successive applications off. Thedelfunction 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. |
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"