> 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/c02/ft_strncpy.md).

# ft\_strncpy

**Objective:**

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

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

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

**Allowed Functions:** None

**Prototype:**

```c
char *ft_strncpy(char *dest, char *src, unsigned int n);
```

***

#### **Detailed Explanation:**

**1. Functionality:**

* The `ft_strncpy` function copies up to `n` characters from the string `src` to the string `dest`.
* If `src` contains fewer than `n` characters, the remainder of `dest` is padded with null characters (`'\0'`).
* Unlike `ft_strcpy`, this function allows you to limit the number of characters copied, making it safer for fixed-size buffers.

**2. Input Parameters:**

* `char *dest`: A pointer to the destination buffer where the string will be copied.
* `char *src`: A pointer to the source string to be copied.
* `unsigned int n`: The maximum number of characters to copy.

**3. Output:**

* A pointer to the destination string (`dest`), which now contains up to `n` characters from `src`.

***

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

1. **Initialize a Counter:**
   * Start at the beginning of both `src` and `dest` with a counter variable (e.g., `i`).
2. **Copy Characters from `src` to `dest`:**
   * Copy characters from `src` to `dest` one at a time until:
     * You reach the null terminator of `src`, or
     * You’ve copied `n` characters.
3. **Pad with Null Characters:**
   * If `src` is shorter than `n`, fill the remaining space in `dest` with null characters (`'\0'`).
4. **Return the Destination Pointer:**
   * Return the pointer to `dest`.

#### **Code Explanation:**

```c
char	*ft_strncpy(char *dest, char *src, unsigned int n)
{
	unsigned int	i;

	i = 0;
	while (i < n && src[i] != '\0') // Copy up to `n` characters or until the end of `src`
	{
		dest[i] = src[i];
		i++;
	}
	while (i < n) // If `src` ends before `n`, pad `dest` with null characters
	{
		dest[i] = '\0';
		i++;
	}
	return (dest); // Return the pointer to the destination string
}
```

***

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

1. **Initialize the Counter:**
   * `unsigned int i = 0;` is used to track the current index of both `src` and `dest`.
2. **First `while` Loop – Copy Characters:**
   * `while (i < n && src[i] != '\0')`
     * This loop copies characters from `src` to `dest` one by one.
     * It continues until either `n` characters are copied or the null terminator (`'\0'`) of `src` is reached.
   * Example:
     * Input: `src = "42Berlin", n = 10`
     * After this loop, `dest = "42Berlin"` (partially filled).
3. **Second `while` Loop – Pad with Null Characters:**
   * `while (i < n)`
     * If the `src` string is shorter than `n`, this loop fills the remaining space in `dest` with null characters (`'\0'`).
   * Example:
     * Input: `src = "42Berlin", n = 10`
     * After this loop, `dest = "42Berlin\0\0"` (fully padded).
4. **Return the Destination Pointer:**
   * `return (dest);`
   * This allows the function to return the pointer to the destination string.

***

#### **Example Usage:**

```c
#include <stdio.h>

char	*ft_strncpy(char *dest, char *src, unsigned int n);

int	main(void)
{
	char src[] = "Hello, 42!";
	char dest[20]; // Ensure the destination array is large enough

	ft_strncpy(dest, src, 5); // Copy only the first 5 characters
	printf("Result: %s\n", dest);

	ft_strncpy(dest, src, 15); // Copy up to 15 characters (padding with '\0')
	printf("Result: %s\n", dest);

	return (0);
}
```

**Expected Output:**

```makefile
Result: Hello
Result: Hello, 42!
```

***

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

1. **Source is Shorter than `n`:**
   * Input: `src = "42", n = 5`
   * Expected Output: `dest = "42\0\0\0"`
2. **Source is Longer than `n`:**
   * Input: `src = "42Berlin", n = 5`
   * Expected Output: `dest = "42Ber"`
3. **Exact Size Match:**
   * Input: `src = "42", n = 2`
   * Expected Output: `dest = "42"`
4. **Empty Source String:**
   * Input: `src = "", n = 5`
   * Expected Output: `dest = "\0\0\0\0\0"`
5. **`n = 0`:**
   * Input: `src = "42Berlin", n = 0`
   * Expected Output: `dest` remains unchanged.

***

#### **Limitations of `ft_strncpy`:**

1. **Non-Null Terminated Strings:**
   * If the source string is longer than or equal to `n`, `dest` may not be null-terminated.
   * This is expected behavior for `strncpy` but might lead to unexpected results when printing or processing strings.


---

# 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/c02/ft_strncpy.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.
