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

# ft\_strncat

**Objective:**

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

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

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

**Allowed Functions: None**

**Prototype:**

```c
char *ft_strncat(char *dest, char *src, unsigned int nb);
```

***

#### **Detailed Explanation:**

**1. Functionality:**

The `ft_strncat` function appends up to `nb` characters from 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`.
* `unsigned int nb`: The maximum number of characters to append from `src`.

**3. Output:**

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

***

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

1. **Find the Length of `dest`:**
   * Use the `ft_strlen` function to determine the length of `dest`.
2. **Check if `nb` is Valid:**
   * If `nb` is less than 1, return `dest` immediately (no characters to append).
3. **Append `src` to `dest`:**
   * Use a loop to copy up to `nb` characters from `src` to `dest`, starting from the end of `dest`.
4. **Add the Null Terminator:**
   * After appending `src`, add a null terminator (`\0`) to the end of the concatenated string.
5. **Return the Destination Pointer:**
   * Return the pointer to `dest` for use by the calling function.

***

#### **Code Explanation:**

```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
}

char	*ft_strncat(char *dest, char *src, unsigned int nb)
{
	unsigned int	i;
	unsigned int	dest_len;

	// Get the length of the destination string
	dest_len = ft_strlen(dest);

	// Initialize the counter for src
	i = 0;

	// If nb is less than 1, return dest immediately
	if (nb < 1)
		return (dest);

	// Append up to nb characters from src to dest
	while (src[i] != '\0' && i < nb)
	{
		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 pointer to the destination string
	return (dest);
}
```

***

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

1. **Find the Length of `dest`:**
   * `dest_len = ft_strlen(dest);`
   * The `ft_strlen` function calculates the length of `dest` by iterating until the null terminator (`\0`) is found.
2. **Check if `nb` is Valid:**
   * If `nb < 1`, the function immediately returns `dest` because no characters need to be appended.
3. **Append `src` to `dest`:**
   * The `while (src[i] != '\0' && i < nb)` loop copies up to `nb` characters from `src` to `dest`, starting from the end of `dest`.
   * `dest[dest_len + i] = src[i];` copies the current character from `src` to `dest`.
   * `i` is incremented after each character is copied.
4. **Add the Null Terminator:**
   * After copying up to `nb` characters, the null terminator (`\0`) is added to the end of `dest` with `dest[dest_len + 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_strncat(char *dest, char *src, unsigned int nb);

int	main(void)
{
	char dest[20] = "Hello, ";
	char src[] = "42 School!";
	unsigned int nb = 3; // Append only 3 characters from src

	ft_strncat(dest, src, nb);

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

	return (0);
}
```

**Expected Output:**

```
Concatenated String: Hello, 42
```

***

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

1. **Empty `dest` String:**
   * Input: `dest = ""`, `src = "42!"`, `nb = 3`
   * Expected Output: `dest = "42!"`
2. **Empty `src` String:**
   * Input: `dest = "Hello, "`, `src = ""`, `nb = 5`
   * Expected Output: `dest = "Hello, "`
3. **`nb` is 0:**
   * Input: `dest = "Hello, "`, `src = "42!"`, `nb = 0`
   * Expected Output: `dest = "Hello, "`
4. **`nb` Larger than `src`:**
   * Input: `dest = "Hello, "`, `src = "42!"`, `nb = 10`
   * Expected Output: `dest = "Hello, 42!"`
5. **Special Characters in `src`:**
   * Input: `dest = "Hello, "`, `src = "42!\nWorld\t"`, `nb = 6`
   * Expected Output: `dest = "Hello, 42!\nW"`

***

#### **Limitations of `ft_strncat`:**

* 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.
* Always ensure that `dest` has sufficient space to hold the original string plus up to `nb` characters from `src`.


---

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