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

# ft\_putstr

**Objective:**

Create a function that displays a string of characters on the standard output.

***

**Turn-in Directory:**

`ex01/`

***

**Files to Turn In:**

`ft_putstr.c`

***

**Allowed Functions:**

`write`

***

**Prototype:**

```c
void ft_putstr(char *str);
```

***

#### **Detailed Explanation**

The `ft_putstr` function takes a string `str` as input and displays it character by character on the standard output using the `write` function. It uses a helper function `ft_putchar` to print each character.

***

#### **Code Implementation**

```c
void ft_putchar(char c)
{
    write(1, &c, 1);  // Write the character 'c' to the standard output
}

void ft_putstr(char *str)
{
    int c;

    c = 0;
    while (str[c] != '\0')  // Loop through the string until the null terminator is found
    {
        ft_putchar(str[c]);  // Print the current character
        c++;  // Move to the next character
    }
}
```

***

#### **Example Usage**

```c
#include <unistd.h>

void ft_putstr(char *str);

int main(void)
{
    ft_putstr("Hello, 42!\n");  // Output: Hello, 42!
    ft_putstr("C programming is fun!\n");  // Output: C programming is fun!
    ft_putstr("");  // Output: (nothing, empty string)
    return 0;
}
```

***

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

1. **Empty String**:
   * `""` → Prints nothing.
2. **String with Spaces**:
   * `"hello world"` → Prints `hello world`.
3. **String with Special Characters**:
   * `"hello\nworld"` → Prints `hello` followed by a newline and `world`.
4. **String with Null Terminator in the Middle**:
   * `"hello\0world"` → Prints `hello` (stops at the first null terminator).

***

#### **Limitations of ft\_putstr**

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. **Helper Function: `ft_putchar`**:
   * This function takes a single character `c` and writes it to the standard output using the `write` system call.
   * `write(1, &c, 1)` writes 1 byte (the character `c`) to file descriptor `1` (standard output).
2. **Main Function: `ft_putstr`**:
   * The function initializes a counter `c` to `0`.
   * It loops through the string `str` until it reaches the null terminator (`\0`).
   * For each character, it calls `ft_putchar` to print the character.
   * The counter `c` is incremented to move to the next character.

***

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

1. **Null Terminator (`\0`)**:
   * The loop stops when it encounters the null terminator in the string.
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 print nothing 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 `printf` or `puts` 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_putstr.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.
