isprint() in C (original) (raw)

Last Updated : 16 Aug, 2024

The **C **isprint() function is used to check if a character passed as the argument is a printable character or not. isprint() is defined in the ****<ctype.h>** standard library header file.

**Printable characters in C are:

**Note: The isprint() function checks the ASCII value of a character to determine whether it is a printable character or not.

Syntax of isprint()

int **isprint (int _c);

Parameters

Return Value

Complexity Analysis

Example of isprint()

The below C program calculates the number of printable characters in the string.

**Algorithm

  1. Traverse the given string character by character up to its length, and check if the character is a printable character using isprint() function.
  2. If it is a printable character, increment the counter by 1, else traverse to the next character.
  3. Print the value of the counter.

Sample Input : string = 'My name \n is \n Ayush'
Sample Output : 18

Implementation

C `

// C program to count printable characters in a string #include <ctype.h> #include <stdio.h> #include <string.h>

// function to calculate printable characters void space(char* str) { int count = 0; int length = strlen(str); for (int i = 0; i < length; i++) { int c = str[i]; // Check if the character is printable if (isprint(c)) { printf("%c", c); count++; } } printf("\n"); printf("%d", count); }

// Driver Code int main() { char str[] = "My name \n is \n Ayush"; space(str); return 0; }

`

Output

My name is Ayush 18