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

# ft\_str\_is\_printable

**Objective:**

Create a function that checks whether a string contains only printable characters. A printable character is any character from the ASCII range `32` (space) to `126` (tilde `~`). The function should return `1` if the string contains only printable characters (or is empty) and `0` otherwise.

***

**Turn-in Requirements:**

* **Directory:** `ex06/`
* **File:** `ft_str_is_printable.c`
* **Allowed Functions:** None

***

**Prototype:**

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

***

**Implementation:**

Here’s how you can implement the function:

```c
// File: ft_str_is_printable.c

int ft_str_is_printable(char *str)
{
    int i;

    i = 0;
    while (str[i] != '\0') // Loop through each character of the string
    {
        // Check if the character is not within the printable ASCII range
        if (str[i] < ' ' || str[i] > '~')
            return (0); // Return 0 if a non-printable character is found
        i++;
    }
    return (1); // Return 1 if all characters are printable or the string is empty
}
```

***

**Explanation:**

* **Input Parameter:**\
  The function takes a single parameter, `str`, which is a pointer to the first character of the input string.
* **Loop through the string:**\
  The `while` loop iterates through the string until it reaches the null terminator (`\0`).
* **Check each character:**
  * The condition ensures that each character is within the printable range (`' '` to `'~'`).
  * If a character falls outside this range, the function immediately returns `0`.
* **Empty string:**\
  If the string is empty (i.e., `str[0] == '\0'`), the loop doesn’t execute, and the function returns `1`.

***

**Example Usage:**

```c
#include <stdio.h>

int ft_str_is_printable(char *str);

int main()
{
    printf("%d\n", ft_str_is_printable("Hello!"));          // Output: 1
    printf("%d\n", ft_str_is_printable("Hello\nWorld"));    // Output: 0
    printf("%d\n", ft_str_is_printable(""));                // Output: 1
    printf("%d\n", ft_str_is_printable("42Berlin~"));       // Output: 1
    printf("%d\n", ft_str_is_printable("\tHello"));         // Output: 0
    return 0;
}
```

* **Explanation of Outputs:**
  * `"Hello!"`: All characters are printable → returns `1`.
  * `"Hello\nWorld"`: Contains the non-printable newline character () → returns `0`.
  * `""`: Empty string → returns `1`.
  * `"42Berlin~"`: All characters are printable → returns `1`.
  * `"\tHello"`: Contains the non-printable tab character () → returns `0`.

***

**Notes:**

* Remove or comment out the `main` function when submitting your code.
* Ensure your code adheres to the Norminette coding standards.
* Avoid including unnecessary files in your directory.


---

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