isprint() function in C
0 244
The isprint() function in C is used to check whether a character is printable or not.
It belongs to the <ctype.h>
Steps:
Checking if a Letter is Ready to Be Printed:
Imagine you have a letter, say 'A'. isprint() will examine it and say, "Yes, this letter is ready to be printed!"
Inspecting a Whole Sentence:
If you have a whole sentence, isprint() will go through each character, one by one, and tell you which ones are ready for printing and which ones are not.
Fixing a Broken Sentence:
Let's say you have a sentence with some strange characters that cannot be printed.
You can use isprint() to find them and replace them with printable ones, like a detective fixing a broken code.
Syntax:
int isprint(int c);
Parameters:
c: It's an integer representing a character.
Return Value:
The isprint() function returns an integer value:
Returns a non-zero value if the character c is printable.
Returns 0 otherwise.
Examples:
1 Check if a Character is Printable:
// program to check if character is printable #include<stdio.h>#include<ctype.h> int main() { char ch = 'A'; if (isprint(ch)) { printf("%c is printable\n", ch); } else { printf("%c is not printable\n", ch); } return 0; }
Output:
A is printable
2 Check if Characters in a String are Printable:
// Program to check if characters in a string are printable #include<stdio.h>#include<ctype.h> int main() { char str[] = "Hello!\n"; for (int i = 0; str[i] != '\0'; ++i) { if (isprint(str[i])) { printf("%c is printable\n", str[i]); } else { printf("%c is not printable\n", str[i]); } } return 0; }
Output:
H is printable e is printable l is printable l is printable o is printable ! is printable
3 Convert Non-Printable Characters to Printable Ones:
// program to convert non-printable characters to printable ones. #include<stdio.h>#include<ctype.h> int main() { char str[] = "Hello!\n"; for (int i = 0; str[i] != '\0'; ++i) { if (!isprint(str[i])) { str[i] = '.'; } } printf("After converting non-printable characters: %s\n", str); return 0; }
Output:
After converting non-printable characters: Hello!.
Share:
Comments
Waiting for your comments