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

# ft\_putstr\_non\_printable

**Objective**

The function `ft_putstr_non_printable` displays a given string on the screen. If the string contains **non-printable characters**, they are displayed in **hexadecimal format**, preceded by a backslash (`\`).

***

### **Turn-in Requirements**

* **Directory:** `ex11/`
* **File:** `ft_putstr_non_printable.c`
* **Allowed Functions:** `write`

***

### **Prototype**

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

***

### **Implementation**

```c
#include <unistd.h>

void	ft_putchar(char c)
{
	write(1, &c, 1);
}

void	ft_putstr_non_printable(char *str)
{
	int		i;
	char		*hex;

	hex = "0123456789abcdef"; // Hexadecimal characters
	i = 0;
	while (str[i] != '\0')
	{
		if (str[i] >= 32 && str[i] <= 126) // If the character is printable
			ft_putchar(str[i]);
		else
		{
			ft_putchar('\\'); // Print backslash
			ft_putchar(hex[(unsigned char)str[i] / 16]); // Print first hex digit
			ft_putchar(hex[(unsigned char)str[i] % 16]); // Print second hex digit
		}
		i++;
	}
}
```

***

### **Explanation**

#### **Input Parameter:**

* The function receives `str`, a **string** containing printable and non-printable characters.

#### **Processing Steps:**

1. **Loop through the string**
   * Iterate through each character until reaching `'\0'`.
2. **Check if the character is printable**
   * ASCII **printable characters** range from **32 (' ') to 126 ('\~')**.
   * If the character is printable, print it using `write()`.
3. **Handle non-printable characters**
   * If a character is **non-printable**, it is converted into a **hexadecimal format**:
     * Print a **backslash (`\`)**.
     * Extract the **first hex digit** using division by 16.
     * Extract the **second hex digit** using modulo 16.
4. **Print the formatted characters**
   * The `write()` function is used to output each character.

***

### **Example Usage**

```c
#include <stdio.h>

void ft_putstr_non_printable(char *str);

int main()
{
    char str1[] = "Coucou\ntu vas bien ?";
    char str2[] = "Hello\x01\x02\x7FWorld!";
    char str3[] = "";

    printf("Original: Coucou\\ntu vas bien ?\nOutput: ");
    ft_putstr_non_printable(str1);
    printf("\n");

    printf("Original: Hello\x01\x02\x7FWorld!\nOutput: ");
    ft_putstr_non_printable(str2);
    printf("\n");

    printf("Original: (empty string)\nOutput: ");
    ft_putstr_non_printable(str3);
    printf("\n");

    return 0;
}
```

#### **Expected Output**

```vbnet
Original: Coucou\ntu vas bien ?
Output: Coucou\0atu vas bien ?

Original: Hello\x01\x02\x7FWorld!
Output: Hello\01\02\7fWorld!

Original: (empty string)
Output: 
```

***

### **Edge Cases Considered**

✅ **Handles empty strings** (prints nothing).\
✅ **Handles all non-printable ASCII characters** (`0-31` and `127`).\
✅ **Preserves original string formatting** while replacing non-printable characters.

***

### **Complexity Analysis**

* **Time Complexity:** **O(n)** → Iterates through the string once.
* **Space Complexity:** **O(1)** → Uses only a small amount of additional memory for `hex`.


---

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