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

# ft\_strlcpy

**Objective**

The function `ft_strlcpy` replicates the behavior of `strlcpy`, a safer alternative to `strcpy`. It copies a string while ensuring null termination and avoiding buffer overflows.

### **Turn-in Requirements**

* **Directory:** `ex10/`
* **File:** `ft_strlcpy.c`
* **Allowed Functions:** None

### **Prototype**

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

***

### **Implementation**

```c
unsigned int	ft_strlcpy(char *dest, char *src, unsigned int size)
{
	unsigned int	i;
	unsigned int	src_len;

	src_len = 0;
	while (src[src_len]) // Calculate the length of src
		src_len++;

	if (size == 0) // If size is 0, return the length of src without modifying dest
		return (src_len);

	i = 0;
	while (src[i] && i < size - 1) // Copy characters from src to dest
	{
		dest[i] = src[i];
		i++;
	}
	dest[i] = '\0'; // Ensure null termination

	return (src_len);
}
```

***

### **Explanation**

#### **Input Parameters:**

1. `dest`: The destination buffer where the `src` string will be copied.
2. `src`: The source string to be copied.
3. `size`: The maximum number of characters to copy, including the null terminator.

#### **Processing Steps:**

1. **Calculate `src_len`**
   * The function first iterates through `src` to determine its length.
2. **Check if `size` is 0**
   * If `size == 0`, the function **returns the length of `src`** without modifying `dest`.
3. **Copy Characters from `src` to `dest`**
   * The function copies characters from `src` to `dest` **up to `size - 1`** to ensure there is space for the null terminator.
4. **Null-Termination**
   * After copying, the function appends `'\0'` to `dest` to prevent overflow.
5. **Return Value**
   * The function **returns the total length of `src`**, allowing the caller to determine if truncation occurred.

***

### **Example Usage**

```c
#include <stdio.h>

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

int main()
{
    char dest[20];
    char *src = "Hello, 42Berlin!";
    unsigned int len;

    len = ft_strlcpy(dest, src, sizeof(dest));
    printf("Copied string: %s\n", dest);
    printf("Length of source: %u\n", len);

    return 0;
}
```

#### **Expected Output**

```yaml
string: Hello, 42Berlin!
Length of source: 17
```

***

### **Edge Cases Considered**

✅ **If `size == 0`**, `dest` is not modified, but the function still returns `src` length.\
✅ Handles cases where `size` is smaller than `src` to prevent buffer overflow.\
✅ Ensures proper null termination (`'\0'`) for safe string handling.

***

### **Complexity Analysis**

* **Time Complexity:** **O(n)** → The function iterates through `src` **twice** (once for length calculation, once for copying).
* **Space Complexity:** **O(1)** → The function operates **in place** without additional memory.


---

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