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:
- digits ( 0123456789 )
- uppercase letters ( ABCDEFGHIJKLMNOPQRSTUVWXYZ )
- lowercase letters ( abcdefghijklmnopqrstuvwxyz )
- punctuation characters ( !”#$%&'()*+,-./:;?@[\]^_`{ | }~ )
- space ( )
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
- c: It is the character to be checked.
Return Value
- It returns a non-zero value(true) if the given character is printable.
- If the character is not printable, it returns zero (false).
Complexity Analysis
- Time Complexity: O(1)
- Auxiliary Space: O(1)
Example of isprint()
The below C program calculates the number of printable characters in the string.
Algorithm
- Traverse the given string character by character up to its length, and check if the character is a printable character using isprint() function.
- If it is a printable character, increment the counter by 1, else traverse to the next character.
- Print the value of the counter.
Sample Input : string = ‘My name \n is \n Ayush’
Sample Output : 18
Implementation
C
#include <ctype.h>
#include <stdio.h>
#include <string.h>
void space( char * str)
{
int count = 0;
int length = strlen (str);
for ( int i = 0; i < length; i++) {
int c = str[i];
if (isprint(c)) {
printf ( "%c" , c);
count++;
}
}
printf ( "\n" );
printf ( "%d" , count);
}
int main()
{
char str[] = "My name \n is \n Ayush" ;
space(str);
return 0;
}
|
OutputMy name is Ayush
18
This article is contributed by Ayush Saxena. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.