isprint() in C++
In C++, isprint() is a predefined function used for string and character handling. cstring is the header file required for string functions and cctype is the headerfile required for character functions.
This function is used to check if the argument contains any printable characters. There are many types of printable characters in C++ such as:
- digits ( 0123456789 )
- uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
- lowercase letters ( abcdefghijklmnopqrstuvwxyz )
- punctuation characters ( !”#$%&'()*+,-./:;?@[\]^_`{ | }~ )
- space ( )
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).
Given a string in C++, we need to calculate the number of printable characters in the string.
Algorithm
- Traverse the given string character by character upto its length, check if character is a printable character.
- If it is a printable character, increment the counter by 1, else traverse to the next character.
- 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:
18
This article is contributed by Ayush Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Different ways to use Const with Reference to a Pointer in C++
- std::to_address in C++ with Examples
- Program to create Custom Vector Class in C++
- std::is_trivially_copy_constructible in C/C++
- Difference between Python and C++
- tgamma() method in C/C++ with Examples
- boost::type_traits::is_array Template in C++
- boost is_pointer template in C++
- Modulo Operator (%) in C/C++ with Examples
- fpclassify() method in C/C++ with Examples
- Operator Overloading '<<' and '>>' operator in a linked list class
- How to find the Entry with largest Value in a C++ Map
- Visibility Modes in C++ with Examples
- Speed up Code executions with help of Pragma in C/C++

