C Library Function ispunct( )


In this tutorial, you will learn about C library function ispunct() which is a character handling function used to check whether the argument supplied is a printing character other than space, a digit or a letter.
C Library Function ispunct( )

As discussed earlier, we have to include ctype.h header file for using ispunct( ) standard character library function.

#include <ctype.h>

C Library Function ispunct( ): Function prototype


int ispunct( int ch);

This function checks whether its argument ch is a printing character other than space, a digit or a letter.

Punctuation characters
$, #, (, ), [, ], {, }, ;, : or %

This function returns a true value if its argument is a printings character and zero (0) otherwise.

Demonstration of C library function ispunct( )

#include <stdio.h>
#include <ctype.h>

int main()
{
   printf("Demonstration of ispunct():\n");
   printf("\n%s%s", ispunct(':') ? ": is a " : ": is not a ", "punctuation character");
   printf("\n%s%s", ispunct('[') ? "[ is a " : "[ is not a ", "punctuation character");
   printf("\n%s%s", ispunct('G') ? "G is a " : "G is not a ", "punctuation character");

   return 0;
}

Output

Demonstration of ispunct():

: is a punctuation character
[ is a punctuation character
G is not a punctuation character

Explanation
In the above example, standard character library function ispunct determines whether its argument is a printing character or not except space, a digit or a letter.

Conditional operator ?: does the selection between two strings “is a” and “is not a”.

This can be simply done with the help of if...else statements.

Example of C library function ispunct() using if … else


#include <stdio.h>
#include <ctype.h>

int main()
{
   printf("Demonstration of ispunct( ):\n");

   if( ispunct(':') == 0)
     printf("\n: is not punctuation character");
   else
     printf("\n: is a punctuation character");

   if( ispunct('[') == 0)
     printf("\n[ is not punctuation character");
   else
     printf("\n[ is a punctuation character");

   if( ispunct('G') == 0)
     printf("\nG is not punctuation character");
   else
     printf("\nG is a punctuation character");

 return 0;
}

Output

Demonstration of ispunct():

: is a punctuation character
[ is a punctuation character
G is not a punctuation character

Explanation
In this example, we have used if...else statements instead of a conditional operator.

Here, if...else statement does the selection between two strings.