isprint() Working with C++


Isprint() in C++ is inbuilt function in “cctype.h” header file which checks whether the character is printable or not.

Isprint returns true for constant cases as Isprint aside from the house character (' '), that returns true.

A locale-specific model version of this function (Isprint) exists in cctype header file.

-Isprint() function can be used to check any Non-Printing character in a series of sentences.

-Isprint() is an Inbuilt function that provides efficient way to handle non printing characters

-Isprint() helps to minimize the lines of code for programmer.

-Isprint() is in true sense decreases the compilation time of program.

Including cctype.h in your program not only allows user to use isprint() but unlocks many other related functions too. Some more functions included in cctype.h are −

  • isblank (Check if character is blank )
  • Iscntrl (Check if character is a control character)
  • isdigit (Check if character is decimal digit )
  • Isgraph( Check if character has graphical representation )

Syntax

Syntax of Isprint() is as follows −

Int isprint (int c);

“A printable character is a character that occupies a printing position on a display” .

Parameters of Isprint() are

C is a character to be checked, casted as an int or EOF.

Example

Input-: first line /n second line /n
Output-: first line
Input-: line one /n line two/n line three /n
Output-: line one

Explanation − It will only print one line because the newline character is not printable.

Example

 Live Demo

/* isprint example */
#include <stdio.h>
#include <ctype.h>
int main () {
   int i=0;
   char str[]="first line n second line n";
   while (isprint(str[i])) {
      putchar (str[i]);
      i++;
   }
   return 0;
}

Output

If we run the above code it will generate the following output −

first line n second line n

Example

 Live Demo

#include <cctype>
#include <iostream>
#include <cstring>
using namespace std;
int main() {
   char str[] = "Hellotallnhow are you";
   for (int i=0; i<strlen(str); i++) {
      if (!isprint(str[i]))
      str[i] = ' ';
   }
   cout << str;
   return 0;
}

Output

If we run the above code it will generate the following output −

Hellotallnhow are you

Updated on: 20-Jan-2020

210 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements