Isprint In C++

isprint is a predefined function in C++ that handles strings and characters. The header files needed for string and character functions are cstring and cctype, respectively. If the argument has any printable characters, this function is utilised to determine that fact. In C++, there are numerous varieties of printable characters, including:

Example

digits ( 0123456789 )
uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
lowercase letters ( abcdefghijklmnopqrstuvwxyz )
punctuation characters ( !"#$%&'()*+,-./:;?@[\]^_`{ | }~ )
space ( )

The built-in C++ function "isprint" in the "cctype.h" header file determines whether a character is printable or not.

With the exception of the house character (' '), Isprint returns true for constant cases.

This function (Isprint) has a localised model version that may be found in the cctype header file.

To determine whether any Non-Printing characters appear in a string of sentences, use the Isprint function.

-Isprint is an internal function that offers an effective method of handling characters that won't print.

Programmers can reduce the number of lines of code by using -Isprint.

It is true that -Isprint speeds up software compilation.

Include cctype.h in your software to enable several additional related functions in addition to isprint for the user. There are additional functions in cctype.h, including

isblank (Check if character is blank ) (Check if character is blank )

Iscntrl (Check if character is a control character) (Check if character is a control character)

isdigit (Check if character is decimal digit ) (Check if character is decimal digit )

Isgraph( Check if character has graphical representation ) ( Check if character has graphical representation )

Syntax:

Example

int isprint( int c ); 
c : character to be checked.
Returns a non-zero value(true) if c is a printable 
character else, zero (false).

Time Complexity: O(n)

Auxiliary Space: O(1)

Given a string in C++, we need to calculate the number of printable characters in the string. Algorithm

1) Traverse the given string character by character upto its length, check if character is a printable character.

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.

Examples:

Example

Input : string = 'My name \n is \n Ayush'
Output : 18
Input :string = 'I live in \n Dehradun'
Output : 19
Example

// CPP program to count printable characters in a string
#include <iostream>
#include <cstring>

#include <cctype>
using namespace std;

// function to calculate printable characters
void space(string& str)
{
	int count = 0;
	int length = str.length();
	for (int i = 0; i< length; i++) {
		int c = str[i];
		if (isprint(c))
			count++;
	}
	cout << count;
}

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

Output:

Parameters of Isprint are

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

Example

Example

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

Due to the newline character's inability to be printed, it will only print one line.

Example

Example

/* 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

Example

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

Example

Hellotallnhow are you

Notes Like all other functions from <cctype>, the behavior of std::isprint is undefined if the argument's value is neither representable as unsigned char nor equal to EOF. To use these functions safely with plain chars (or signed chars), the argument should first be converted to unsigned char:

Example

bool my_isprint(char ch)
{

    return std::isprint(static_cast<unsigned char>(ch));
}

Similarly, they should not be directly used with standard algorithms when the iterator's value type is char or signed char. Instead, convert the value to unsigned char first:

Example

int count_prints(const std::string& s)
{
    return std::count_if(s.begin(), s.end(), 
                      // static_cast<int(*)(int)>(std::isprint)         // wrong
                      // [](int c){ return std::isprint(c); }           // wrong
                      // [](char c){ return std::isprint(c); }          // wrong
[](unsigned char c){ return std::isprint(c); } // correct
                        );
}

Input Required

This code uses input(). Please provide values below: