> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c03/ft_strlcat.md).

# ft\_strlcat

**Objective:**

Recreate the behavior of the `strlcat` function (refer to `man strlcat`).

**Turn-in Directory: `ex05/`**

**Files to Turn In: `ft_strlcat.c`**

**Allowed Functions: None**

**Prototype:**

```c
unsigned int ft_strlcat(char *dest, char *src, unsigned int size);
```

***

#### **Detailed Explanation:**

**1. Functionality:**

The `ft_strlcat` function concatenates the string `src` to the end of the string `dest`, ensuring that the total length of the concatenated string does not exceed `size - 1` characters. It then adds a null terminator (`\0`) and returns the total length of the string it tried to create (the length of `dest` plus the length of `src`).

**2. Input Parameters:**

* `char *dest`: A pointer to the destination string where `src` will be appended.
* `char *src`: A pointer to the source string to be appended to `dest`.
* `unsigned int size`: The total size of the destination buffer (`dest`).

**3. Output:**

* Returns the total length of the string it tried to create (the length of `dest` plus the length of `src`).
* If `size` is smaller than or equal to the length of `dest`, it returns `size + src_len` (indicating truncation).

***

#### **Implemented Code:**

```c
int	ft_strlen(char *str)
{
	int	i;

	i = 0;
	while (str[i] != '\0') // Loop until the null terminator is found
		i++;
	return (i); // Return the length of the string
}

unsigned int	ft_strlcat(char *dest, char *src, unsigned int size)
{
	unsigned int	i;
	unsigned int	dest_len;
	unsigned int	src_len;

	// Calculate the lengths of dest and src
	dest_len = ft_strlen(dest);
	src_len = ft_strlen(src);

	// If size is 0 or dest is already larger than size, return the required length
	if (size <= dest_len)
		return (size + src_len);

	// Append src to dest, ensuring we don't exceed size - 1 characters
	i = 0;
	while (src[i] != '\0' && (dest_len + i) < (size - 1))
	{
		dest[dest_len + i] = src[i]; // Copy the current character from src to dest
		i++;
	}

	// Add the null terminator to the end of the concatenated string
	dest[dest_len + i] = '\0';

	// Return the total length of the string it tried to create
	return (dest_len + src_len);
}
```

***

#### **Step-by-Step Breakdown:**

1. **Calculate Lengths:**
   * `dest_len = ft_strlen(dest);` calculates the length of `dest`.
   * `src_len = ft_strlen(src);` calculates the length of `src`.
2. **Check if `size` is Too Small:**
   * If `size <= dest_len`, the function returns `size + src_len` without modifying `dest`. This indicates that truncation would occur.
3. **Append `src` to `dest`:**
   * The `while (src[i] != '\0' && (dest_len + i) < (size - 1))` loop appends characters from `src` to `dest`, ensuring that the total length does not exceed `size - 1`.
   * `dest[dest_len + i] = src[i];` copies the current character from `src` to `dest`.
4. **Add the Null Terminator:**
   * After appending characters, the null terminator (`\0`) is added to the end of `dest` with `dest[dest_len + i] = '\0';`.
5. **Return the Total Length:**
   * The function returns `dest_len + src_len`, which is the total length of the string it tried to create.

***

#### **Example Usage:**

```c
#include <stdio.h>

unsigned int	ft_strlcat(char *dest, char *src, unsigned int size);

int	main(void)
{
	char dest[20] = "Hello, ";
	char src[] = "42 School!";
	unsigned int size = sizeof(dest);

	unsigned int result = ft_strlcat(dest, src, size);

	printf("Concatenated String: %s\n", dest);
	printf("Total Length: %u\n", result);

	return (0);
}
```

**Expected Output:**

```
Concatenated String: Hello, 42 School!
Total Length: 17
```

***

#### **Edge Cases to Test:**

1. **`size` is 0:**
   * Input: `dest = "Hello, "`, `src = "42!"`, `size = 0`
   * Expected Output: `dest` remains unchanged, and the function returns `0 + src_len`.
2. **`dest` is Larger than `size`:**
   * Input: `dest = "Hello, "`, `src = "42!"`, `size = 5`
   * Expected Output: `dest` remains unchanged, and the function returns `size + src_len`.
3. **Empty `src` String:**
   * Input: `dest = "Hello, "`, `src = ""`, `size = 20`
   * Expected Output: `dest` remains unchanged, and the function returns `dest_len + 0`.
4. **Empty `dest` String:**
   * Input: `dest = ""`, `src = "42!"`, `size = 20`
   * Expected Output: `dest = "42!"`, and the function returns `0 + src_len`.
5. **Special Characters in `src`:**
   * Input: `dest = "Hello, "`, `src = "42!\nWorld\t"`, `size = 20`
   * Expected Output: `dest = "Hello, 42!\nWorld\t"`, and the function returns `dest_len + src_len`.

***

#### **Limitations of `ft_strlcat`:**

* The function assumes that `dest` has enough space to accommodate the null terminator after appending `src`. If `dest` is too small, it may still result in undefined behavior.
* Always ensure that `dest` has sufficient space to hold the concatenated result.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/c03/ft_strlcat.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
