The isprint function in C++ is a built-in function responsible for processing strings and individual characters. To work with string and character functions, the requisite header files are cstring and cctype, correspondingly. When the input contains any displayable characters, this function is employed to confirm this condition. Within the C++ language, there exist a wide range of printable characters such as:
digits ( 0123456789 )
uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
lowercase letters ( abcdefghijklmnopqrstuvwxyz )
punctuation characters ( !"#$%&'()*+,-./:;?@[\]^_`{ | }~ )
space ( )
The predefined C++ function "isprint" within the "cctype.h" header file is responsible for evaluating if a character is displayable or not.
Isprint returns true for all characters except for the space character (' ').
This function (Isprint) has a localized model variant that can be located in the cctype header file.
To check for the presence of any Non-Printing characters within a string of text, you can employ the Isprint function.
-Isprint is an intrinsic function that provides a reliable approach for managing non-printable characters.
Developers have the option to minimize the amount of code lines by utilizing the -Isprint function.
It is indeed accurate that the -Isprint function accelerates the process of software compilation.
Integrate cctype.h within your software to unlock a variety of supplementary functions beyond isprint for the user. cctype.h features a range of extra functions, such as
The function ```
/ 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;
}
The iscntrl function is utilized to determine whether a specific character is a control character or not.
The isdigit() function is used to verify if a character is a decimal digit.
The isgraph() function checks whether a character has a printable representation.
### Syntax:
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)
Calculate the quantity of printable characters within a given string in the C++ programming language using the following algorithm:
1) Iterate through the provided string character by character until reaching its total length, validate each character to determine if it is a printable character.
If the character is printable, increase the counter by one; otherwise, move on to the next character in the sequence.
3) Print the value of the counter.
### Examples:
Input : string = 'My name \n is \n Ayush'
Output : 18
Input :string = 'I live in \n Dehradun'
Output : 19
// 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 represents a character that will be examined, converted to an integer, or compared to 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
Because the newline character cannot be displayed, it will only output a single line of text.
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 execute the aforementioned code, it will produce the subsequent output -
first line n second line n
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 execute the code mentioned above, it will produce the subsequent output -
Hellotallnhow are you
#### Notes Like all other functions from __PRESERVE_10__, 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:
bool my_isprint(char ch)
{
return std::isprint(static_cast<unsigned char>(ch));
}
Likewise, it is not recommended to directly apply them with regular algorithms in cases where the iterator's value type is char or signed char. In such situations, it is advisable to initially convert the value to unsigned char.
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
);
}