Skip to content

isprint

Marcos edited this page Oct 1, 2024 · 1 revision

Función ft_isprint

La función ft_isprint comprueba si el valor entero pasado como argumento es un carácter imprimible, es decir, si está dentro del rango de valores que representan caracteres imprimibles del código ASCII (de 32 a 126).

Explicación del comando man

ISPRINT(3) (simplified)

NAME
    isprint -- printing character test (space character inclusive)
SYNOPSIS
    int isprint(int c)
DESCRIPTION
    The isprint() function tests for any printing character, including space. The value of the argument must representable as an unsigned char or the value of EOF.
RETURN VALUES
    The isprint() function returns zero if the character tests false and returns non-zero if the character tests true.

Explicación con mis palabras

La función isprint() evalúa si el valor entero pasado como argumento corresponde a un carácter imprimible en el código ASCII, lo que incluye los caracteres desde el espacio (' ') hasta el carácter de tilde inversa ('~'). Si el valor está dentro de este rango, la función devuelve un valor distinto de cero; de lo contrario, devuelve 0.

Implementación en C

Aquí te muestro mi implementación de la función ft_isprint en mi proyecto de Libft.

Código de ft_isprint

int	ft_isprint(int c)
{
	return (c >= 32 && c <= 126);
}

Main para probar ft_isprint

El siguiente código es un ejemplo de cómo puedes probar la función ft_isprint():

#include <stdio.h>

int	ft_isprint(int c);

int main(void)
{
	char test1 = 'A';
	char test2 = 31;
	char test3 = ' ';

	printf("Test con 'A': %d\n", ft_isprint(test1)); // Debe devolver 1
	printf("Test con 31: %d\n", ft_isprint(test2)); // Debe devolver 0
	printf("Test con ' ': %d\n", ft_isprint(test3)); // Debe devolver 1

	return 0;
}

Clone this wiki locally