C++ program to check if a character is a punctuation using ispunct

C++ program to check if a character is punctuation using ispunct function:

In C++, we can use ispunct() function to check if a character is punctuation or not. It checks that as defined by the current C locale. This is define in header. In this post, we will learn how to use ispunct in C++ to check if a character is punctuation or not.

Definition of ispunct:

ispunct is defined as below:

int ispunct(int ch)

It takes one character to check as the parameter and cast the character to integer or EOF.

It returns one integer value. zero to indicate false and any other value to indicate true.

Example of ispunct:

Below example takes a character as input from the user and prints if it is punctuation or not:

#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    char ch;
    
    cout<<"Enter a character : "<<endl;
    cin>>ch;

    if(ispunct(ch)){
        cout<<"Punctuation character !"<<endl;
    }else{
        cout<<"Not a Punctuation character !"<<endl;
    }
}

It will give output as like below:

Enter a character : 
*
Punctuation character !

Enter a character : 
a
Not a Punctuation character !

You might also like: