> 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_strcat.md).

# ft\_strcat

**Objective:**

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

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

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

**Allowed Functions: None**

**Prototype:**

```c
char *ft_strcat(char *dest, char *src);
```

***

#### **Detailed Explanation:**

**1. Functionality:**

The `ft_strcat` function appends the string `src` (source) to the end of the string `dest` (destination). It overwrites the null terminator (`\0`) of `dest` and adds a new null terminator at the end of the concatenated string.

**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`.

**3. Output:**

* Returns a pointer to the destination string (`dest`), which now contains the concatenated result of `dest` and `src`.

***

#### **Steps to Implement:**

1. **Find the End of `dest`:**
   * Use a loop to find the index of the null terminator (`\0`) in `dest`.
2. **Append `src` to `dest`:**
   * Use another loop to copy characters from `src` to `dest`, starting from the end of `dest`.
3. **Add the Null Terminator:**
   * After appending `src`, add a null terminator (`\0`) to the end of the concatenated string.
4. **Return the Destination Pointer:**
   * Return the pointer to `dest` for use by the calling function.

***

#### **Code Explanation:**

```c
char	*ft_strcat(char *dest, char *src)
{
	int	i;
	int	j;

	// Initialize counters
	i = 0;
	j = 0;

	// Find the end of the destination string (dest)
	while (dest[i])
		i++;

	// Append the source string (src) to the destination string (dest)
	while (src[j])
	{
		dest[i] = src[j]; // Copy the current character from src to dest
		i++;
		j++;
	}

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

	// Return the pointer to the destination string
	return (dest);
}
```

***

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

1. **Initialize Counters:**
   * `int i = 0;` and `int j = 0;`
   * `i` is used to iterate through `dest`, and `j` is used to iterate through `src`.
2. **Find the End of `dest`:**
   * The `while (dest[i])` loop increments `i` until it reaches the null terminator (`\0`) of `dest`.
3. **Append `src` to `dest`:**
   * The `while (src[j])` loop copies each character from `src` to `dest`, starting from the end of `dest`.
   * `dest[i] = src[j];` copies the current character from `src` to `dest`.
   * Both `i` and `j` are incremented after each character is copied.
4. **Add the Null Terminator:**
   * After all characters from `src` are copied, the null terminator (`\0`) is added to the end of `dest` with `dest[i] = '\0';`.
5. **Return the Destination Pointer:**
   * The function returns the pointer to `dest` (`return (dest);`), allowing further operations on it.

***

#### **Example Usage:**

```c
#include <stdio.h>

char	*ft_strcat(char *dest, char *src);

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

	ft_strcat(dest, src);

	printf("Concatenated String: %s\n", dest);

	return (0);
}
```

**Expected Output:**

```c
Concatenated String: Hello, 42!
```

***

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

1. **Empty `dest` String:**
   * Input: `dest = ""`, `src = "42!"`
   * Expected Output: `dest = "42!"`
2. **Empty `src` String:**
   * Input: `dest = "Hello, "`, `src = ""`
   * Expected Output: `dest = "Hello, "`
3. **Both Strings Empty:**
   * Input: `dest = ""`, `src = ""`
   * Expected Output: `dest = ""`
4. **Large `src` String:**
   * Input: `dest = "Hello, "`, `src = "Welcome to 42 School!"`
   * Expected Output: `dest = "Hello, Welcome to 42 School!"`
5. **Special Characters in `src`:**
   * Input: `dest = "Hello, "`, `src = "42!\nWorld\t"`
   * Expected Output: `dest = "Hello, 42!\nWorld\t"`

***

#### **Limitations of `ft_strcat`:**

* The function does not check if `dest` has enough space to accommodate the concatenated result. If `dest` is too small, it can cause a **buffer overflow**, leading to undefined behavior.
* A safer alternative would be to use `ft_strncat`, which limits the number of characters appended.

<br>


---

# 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_strcat.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.
