> 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/c01/ft_strlen.md).

# ft\_strlen

**Objective:**

Create a function that counts and returns the number of characters in a string.

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

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

**Allowed Functions:** None

**Prototype:**

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

***

#### **Detailed Explanation:**

**1. Functionality:**

* The `ft_strlen` function calculates the length of a string by counting the number of characters until it encounters the null terminator (`'\0'`).
* Strings in C are arrays of characters terminated by the special null character (`'\0'`), which marks the end of the string.

**2. Input:**

* A single parameter: `char *str`
  * This is a pointer to the first character of the string to be measured.

**3. Output:**

* An integer value representing the number of characters in the string, excluding the null terminator.

**4. Implementation Steps:**

1. Initialize a counter variable (e.g., `length`) to `0`.
2. Loop through the string using the pointer `str`:
   * Check if the current character is not `'\0'`.
   * Increment the counter for each character.
3. Stop the loop when `'\0'` is encountered.
4. Return the counter.

**5. Code Implementation:**

Here is the code for the `ft_strlen` function:

```c
int ft_strlen(char *str)
{
    int length = 0;

    while (str[length] != '\0') // Continue until the null character is found
    {
        length++; // Increment the length for each character
    }
    return length; // Return the total length of the string
}
```

***

#### **Example Usage:**

**Code Example:**

```c
#include <stdio.h>

int ft_strlen(char *str);

int main(void)
{
    char example[] = "Hello, 42!";
    printf("The length of the string is: %d\n", ft_strlen(example));
    return 0;
}
```

**Expected Output:**

```csharp
The length of the string is: 10
```

***

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

1. **Empty String:**
   * Input: `""`
   * Expected Output: `0`
2. **String with One Character:**
   * Input: `"A"`
   * Expected Output: `1`
3. **String with Special Characters:**
   * Input: `"Hello\nWorld!"`
   * Expected Output: `12` (The newline  counts as a character)
4. **String with Spaces:**
   * Input: `" "`
   * Expected Output: `1`


---

# 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/c01/ft_strlen.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.
