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

# ft\_strlen

**Objective:**

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

***

**Turn-in Directory:**

`ex00/`

***

**Files to Turn In:**

`ft_strlen.c`

***

**Allowed Functions:**

None

***

**Prototype:**

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

***

#### **Detailed Explanation**

The `ft_strlen` function calculates the length of a string `str` by counting the number of characters until it reaches the **null terminator** (`\0`). It returns:

* The **number of characters** in the string (excluding the null terminator).

***

#### **Code Implementation**

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

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

***

#### **Example Usage**

```c
#include <stdio.h>

int ft_strlen(char *str);

int main(void)
{
    printf("Length of 'hello': %d\n", ft_strlen("hello"));  // Output: 5
    printf("Length of '42': %d\n", ft_strlen("42"));       // Output: 2
    printf("Length of '': %d\n", ft_strlen(""));            // Output: 0
    printf("Length of 'C programming': %d\n", ft_strlen("C programming"));  // Output: 13

    return 0;
}
```

***

#### **Edge Cases to Consider**

1. **Empty String**:
   * `""` → Returns `0`.
2. **String with Spaces**:
   * `"hello world"` → Returns `11`.
3. **String with Special Characters**:
   * `"hello\nworld"` → Returns `11` (newline is counted as a character).
4. **String with Null Terminator in the Middle**:
   * `"hello\0world"` → Returns `5` (stops at the first null terminator).

***

#### **Limitations of ft\_strlen**

1. **No Bounds Checking**:
   * If given a non-null-terminated string, the function may lead to **undefined behavior**.
2. **No Handling of Non-String Input**:
   * If the input is not a valid string (e.g., a random memory address), the function will not work as expected.

***

#### **How It Works**

1. **Initialize a Counter**:
   * The variable `i` is initialized to `0` to keep track of the number of characters.
2. **Loop Through the String**:
   * The `while` loop continues as long as the current character (`str[i]`) is not the null terminator (`\0`).
   * For each iteration, `i` is incremented by `1`.
3. **Return the Count**:
   * Once the loop ends (when the null terminator is reached), the function returns the value of `i`, which is the total number of characters in the string.

***

#### **Key Points to Remember**

1. **Null Terminator (`\0`)**:
   * In C, strings are null-terminated, meaning the end of the string is marked by the special character `\0`.
   * The loop stops when it encounters `\0`.
2. **Efficiency**:
   * This implementation is efficient because it only requires a single loop through the string.
   * The time complexity is **O(n)**, where `n` is the length of the string.
3. **Edge Cases**:
   * If the string is empty (i.e., `""`), the function will return `0` because the loop won’t execute (the first character is `\0`).

***

#### **Best Practices**

1. **Adhere to the Norm**:
   * Ensure your code follows the **norminette** guidelines (e.g., no more than 25 lines per function, proper indentation, etc.).
   * Run `norminette` on your file to check for compliance.
2. **Test Edge Cases**:
   * Test your function with an empty string (`""`).
   * Test it with a string that contains only the null terminator (`"\0"`).
3. **Avoid Using Library Functions**:
   * Do not use built-in functions like `strlen` from the C standard library. The goal is to implement the functionality yourself.


---

# 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/c04/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.
